Linux 守护进程停止启动-停止-守护进程

发布于 2024-12-09 12:17:30 字数 1354 浏览 0 评论 0原文

我正在 Linux 中创建一个守护进程。我创建了 init.d 文件,并使用当我尝试停止它时(使用 /etc/init.d/mydaemon stop)成功启动了守护进程进程

/etc/init.d/mydaemon start

,但是,它成功停止,但 start-stop-daemon 似乎从未完成调用 start-stop-daemon

详细模式后没有立即发生回显,表明它停止了该进程,并且查看系统监视器,它确实停止了该进程。

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"
}

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

I have a daemon I am creating in linux. I created the init.d file and have successfully started the daemon process using

/etc/init.d/mydaemon start

When I try to stop it(with /etc/init.d/mydaemon stop), however, it stops successfully, but start-stop-daemon never seems to complete as evidenced by no echos occuring immediately after the call to start-stop-daemon

Verbose mode shows that it stopped the process, and looking at system monitor, it does stop the process.

Stopped mydaemon (pid 13292 13310).

Here is my stop function of the init.d file.

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"
}

I am running this on virtual machine, does this affect anything?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眉目亦如画i 2024-12-16 12:17:30

在虚拟机上运行不应影响此问题。
我不知道为什么会发生这种情况,也不知道它如何接管父脚本的控制权。

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

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

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

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

Running on a virtual machine shouldn't affect this.
And I have no idea why this is happening or how it is taking over control of the parent script.

However, I just encountered this issue and discovered that if I do:

start-stop-daemon ... && echo -n

it will work as expected and relinquish control of the shell.

I have no idea why this works, but it seems to work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文