解決方法: echo + | (管道)
開始使用
echo "admin" | sudo service tomcat7 stop
始終提示輸入密碼
后來查看了下sudo命令的使用
man sudo
發現有如下的解釋:
-S The -S (stdin) option causes sudo to read the password from
the standard input instead of the terminal device. The
password must be followed by a newline character.
因此,之前的命令修改如下:
echo "admin" | sudo -S service tomcat7 stop