How to execute the command after sudo su

2019-09-13 04:33发布

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.

2条回答
在下西门庆
2楼-- · 2019-09-13 05:07

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.

查看更多
地球回转人心会变
3楼-- · 2019-09-13 05:14

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.
查看更多
登录 后发表回答