在我写的一个简单的启动脚本中我有:
PID=`su user_name -c "daemon_name > /dev/null 2>&1 & echo \$!"` echo $PID该脚本以root身份运行。 回声并没有真正回应:)
如果我在shell中输入完全相同的命令,它可以工作:
# su user_name -c "daemon_name > /dev/null 2>&1 & echo \$!" 11831 #有任何想法吗? 谢谢。
编辑:在我尝试以另一个用户身份运行应用程序之前,它工作正常。 我以前使用过这段代码:
PID=`daemon_name > /dev/null 2>&1 & echo $!` echo $PID它以root身份运行良好
in a simple startup script I wrote I have:
PID=`su user_name -c "daemon_name > /dev/null 2>&1 & echo \$!"` echo $PIDThe script is run as root. And the echo doesn't really echo anything :)
If I input the exact same command in the shell, it works:
# su user_name -c "daemon_name > /dev/null 2>&1 & echo \$!" 11831 #Any ideas? Thanks.
EDIT: It was working fine before I tried to run the application as another user. I was previously using this code:
PID=`daemon_name > /dev/null 2>&1 & echo $!` echo $PIDand it worked fine as root
最满意答案
守护进程被设计为多次fork,因此从$返回的PID很可能! 不会是你想要以后使用的pid。 您可能需要执行以下操作:
ps -eo pid,command,lstart --sort lstart | grep 'daemon_name' | tail -1daemons are designed to fork multiple times so likely the PID returned from $! will not be the pid you want to work with later. You will probably have to do something like this:
ps -eo pid,command,lstart --sort lstart | grep 'daemon_name' | tail -1更多推荐
发布评论