0

This question already has an answer here:

I have a script, which starts the execution of multiple C++-programs. I have to execute those with sudo, as they use some "restricted" linux libs/files(probably not the right nomenclature, but you get what i mean). Now i would like to write a shell-script which calls those binaries one after the other, but i do not know how to do this:

#!/bin/bash
./main a > out/output2DFloat.csv

./main a a > out/output3DFloat.csv

./main a a a > out/output2DDouble.csv

./main a a a a > out/output3DDouble.csv

and call this with sudo? Or append Sudo before each and call it with sudo...

My fear is, that the sudo timer runs out, before even one is finished, which would lead to big problems, if the next ones then need sudo-rights ...

marked as duplicate by Kulfy, vanadium, George Udosen, Videonauth, user535733 Mar 30 at 14:38

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

0

You could just write sudo in front of every command, which needs sudo rights. If "the sudo timer runs out", you will be prompted again. This would look like

#!/bin/bash
sudo ./main a > out/output2DFloat.csv

sudo ./main a a > out/output3DFloat.csv

sudo ./main a a a > out/output2DDouble.csv

sudo ./main a a a a > out/output3DDouble.csv

then.

  • yeah, but this whole script will take around 3-4 hrs. and i won't be at my pc all the time ... i also can't run this script as root i think, beacause some libraries are installed in my user space ... but i don't know if they are needed after the compilation process is done – Clebo Sevic Mar 30 at 14:33
0

You can use suid to run your program with root privileges (no need to enter password each time):

Normally, when a program runs under Linux, it inherits the permissions of the user who is running it, thus if I run a program under my account, the program runs with the same permissions that I would have if that program were me. Thus, if I cannot open a certain file, the program I am running also cannot open the file in question.

If I set the SUID or SGID bit for a file, this causes any persons or processes that run the file to have access to system resources as though they are the owner of the file read more...

So you can easily change the ownership of script file:

chown root:user <script file>

Put your username instead of user. Then change the permission of the script file like this:

chmod g+s <script file>
0

To not enter sudo password twice or more, you can use subshell and heredoc, like this:

#!/bin/bash
sudo /bin/bash <<EOF
  ./main a > out/output2DFloat.csv
  ./main a a > out/output3DFloat.csv
  ./main a a a > out/output2DDouble.csv
  ./main a a a a > out/output3DDouble.csv
EOF
  • now it says, that the command sudo was not found – Clebo Sevic Mar 30 at 14:35
  • I`ll check on real script and update answer – LeonidMew Mar 30 at 14:37
  • @CleboSevic here are a fix, I have checked - it working now :) – LeonidMew Mar 30 at 14:45

Not the answer you're looking for? Browse other questions tagged or ask your own question.