How to execute the command after sudo su

五迷三道 提交于 2019-12-25 08:27:25

问题


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.



来源:https://stackoverflow.com/questions/40400763/how-to-execute-the-command-after-sudo-su

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!