How to execute the command after sudo su

2019-09-13 04:49发布

问题:

I need to execute "monit restart haproxy" command after sudo su.

This is my script.sh

sudo su
monit restart haproxy.

Here if i will execute the script.sh file then it will hang and it is not executing the monit restart haproxy command.

Any idea.

Thanks in advanced.

回答1:

The sudo command is expecting you to enter a password which is why it will hang, you can use a pipe so that the password is passed to the command like this:

echo <password> | sudo su
monit restart haproxy.


回答2:

Here comes some information about sudo su. You need to enter password for sudo or su command, here is some quote from link i posted that may be the source of your problem:

sudo - sudo is meant to run a single command with root privileges. But unlike su it prompts you for the password of the current user. This user must be in the sudoers file (or a group that is in the sudoers file). By default, Ubuntu "remembers" your password for 15 minutes, so that you don't have to type your password every time.