简体   繁体   中英

How to execute the command after sudo su

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?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM