2

我有一个在 linux 中创建的守护进程。我创建了 init.d 文件并使用成功启动了守护进程

/etc/init.d/mydaemon start

但是,当我尝试停止它时(使用 /etc/init.d/mydaemon stop),它成功停止,但 start-stop-daemon 似乎永远不会完成,因为在调用 start-stop-守护进程

详细模式显示它停止了进程,并且查看系统监视器,它确实停止了进程。

Stopped mydaemon (pid 13292 13310).

这是我的 init.d 文件的停止功能。

do_stop()
{
    # Return
    #   0 if daemon has been stopped
    #   1 if daemon was already stopped
    #   2 if daemon could not be stopped
    #   other if a failure occurred
    start-stop-daemon --stop --name $NAME -v
echo "stopped"#This is never printed and the script never formally gives shell back.
    RETVAL="$?"

    [ "$RETVAL" = 2 ] && return 2

    # Wait for children to finish too if this is a daemon that forks
    # and if the daemon is only ever run from this initscript.
    # If the above conditions are not satisfied then add some other code
    # that waits for the process to drop all resources that could be
    # needed by services started subsequently.  A last resort is to
    # sleep for some time.
    start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $DAEMON 
    [ "$?" = 2 ] && return 2
    # Many daemons don't delete their pidfiles when they exit.

    return "$RETVAL"
}

我在虚拟机上运行这个,这有什么影响吗?

4

1 回答 1

0

在虚拟机上运行应该不会影响这一点。
而且我不知道为什么会这样,也不知道它是如何接管父脚本的。

但是,我刚刚遇到了这个问题,发现如果我这样做:

启动-停止-守护进程 ... && echo -n

它将按预期工作并放弃对 shell 的控制。

我不知道为什么这有效,但它似乎有效。

于 2014-05-19T00:33:10.143 回答