如何判断Linux守护进程的状态

时间:2009-09-21 10:07:00

标签: linux bash daemon

我们在c中有一个Linux守护进程和一个bash脚本来启动它。由于某些配置文件错误,守护程序有时无法启动,但脚本报告守护程序已成功启动。脚本的片段如下所示,有人可以告诉我脚本有什么问题吗?

...
case "$1" in
start)
echo -n "Starting Demo Daemon: "
sudo -u demouser env DEMO_HOME=$DEMO_HOME /usr/local/demouser/bin/democtl startup > /dev/null 2> /dev/null
if [ "$?" = "0" ]; then
    echo_success
else
    echo_failure
fi
echo
;;
...

谢谢!

2 个答案:

答案 0 :(得分:6)

我觉得脚本没有任何问题,如果无法正常启动,守护进程将返回非零退出状态,并且基于脚本将显示消息。(我认为这样做)

答案 1 :(得分:0)

您可以在脚本中添加以下行以获取Linux守护程序的运行状态

status=`ps -aef |grep "\/usr\/local\/demouser\/bin\/democtl" |grep -v grep|wc -l`
if [ "$status" = "1" ]; then
  echo_success
else
  echo_failure
fi
相关问题