1

I am making a script where I wish to run two services as a certain user. The script should switch to a user and then run a program and after the program has been executed, it should start running the second program.

1

The sudo command can be told to run as a specific user:

 -u user, --user=user
             Run the command as a user other than the default target user
             (usually root).  The user may be either a user name or a
             numeric user ID (UID) prefixed with the ‘#’ character (e.g.
             #0 for UID 0).

So, inside your script, you can use sudo -u USERNAME to run the two services:

sudo -u foo command1
sudo -u foo command2

However, this means that the script itself will need to be run as root. Otherwise, the sudo will ask for the password of the user foo.

  • This is how I was planning to run. su -s /bin/bash user -c /optimal.sh I want to use the sleep command for 2 minutes and then run the another script. how am I going to achieve this. It takes two minutes to finish the executing the first program and after the first program is done running. I want it to start running the second program only after the first program is finished running. How I am going to achieve this. I will be using the sleep command but how should i write it – Mintu Mar 11 '16 at 12:07
  • @Mintu 1) please don't do this. You need to have everything in your question instead of adding each requirement with a comment. 2) I have shown you how to do this. Use sudo -u username command. The second command will only be run after the first one has finished. There is no need for sleep. – terdon Mar 11 '16 at 12:09
  • re: last paragraph: sudo would ask for the password of the user running the sudo command, not the runas user. Also /etc/sudoers can be configured so that user foo can run commands as non-root users without a passwd, just as it can allow foo to run commands as root without a passwd, e.g: foo ALL=(otheruser) NOPASSWD : /path/to/command1 /path/to/command2 – cas Mar 12 '16 at 0:08
1
sudo -u username -s -- "program1 && program2"

or

sudo -u username bash -c 'program1 && program2'
  • This is how I was planning to run. su -s /bin/bash user -c /optimal.sh I want to use the sleep command for 2 minutes and then run the another script. how am I going to achieve this. It takes two minutes to finish the executing the first program and after the first program is done running. I want it to start running the second program only after the first program is finished running. How I am going to achieve this. I will be using the sleep command but how should i write it – Mintu Mar 11 '16 at 12:03
  • No need to use sleep to achieve what you want. You can either start the first program and if finished and executed properly , then start the second program: "program1 && program2" or start the first program and when finished, execute the second program independently if the first one executed properly or not: "program1; program2" – marc Mar 11 '16 at 16:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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