在unix启动脚本中检索pid(retrieving pid in unix startup script)

编程入门 行业动态 更新时间:2024-10-26 12:22:31
在unix启动脚本中检索pid(retrieving pid in unix startup script)

在我写的一个简单的启动脚本中我有:

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 $PID

The 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 $PID

and it worked fine as root

最满意答案

守护进程被设计为多次fork,因此从$返回的PID很可能! 不会是你想要以后使用的pid。 您可能需要执行以下操作:

ps -eo pid,command,lstart --sort lstart | grep 'daemon_name' | tail -1

daemons 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

更多推荐

本文发布于:2023-07-21 09:30:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1208975.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:脚本   pid   unix   script   startup

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!