start-stop-daemon 没有按预期工作,没有写入 pid 文件

dai*_*isy 19 ubuntu daemon start-stop-daemon

我正在尝试控制基于 python 的程序(它不会与控制台分离)

#!/bin/bash

user=nobody
pid=/var/run/xx.pid
name=xx
prog=/xx.py

case $1 in
    start)
        /sbin/start-stop-daemon --start -b --oknodo --user "$user" --name "$name" --pidfile "$pid" --startas "$prog" --chuid nobody -- --daemon
        ;;
    stop)
        /sbin/start-stop-daemon --stop --oknodo --user "$user" --name "$name" --pidfile "$pid" --retry=TERM/5/KILL/1
        ;;
    restart)
        ;;
    *)
        ;;
esac
Run Code Online (Sandbox Code Playgroud)

开始部分工作正常。我可以看到脚本正在运行,但停止部分没有。它简单地说No xx found running; none killed.

所以我猜开始部分有问题?

phe*_*mer 23

start-stop-daemon --start --pidfile "$pid"除非指定了--make-pidfile( -m),否则不会写入 pid 文件。没有--make-pidfile它取决于正在启动的程序来创建它。同样为了--make-pidfile工作,正在启动的进程不能自我守护(通过 fork),因为这样start-stop-daemon就不会知道它应该在文件中放入什么 PID。

唯一--pidfile "$pid"不会在你的应用场景是,它会导致start-stop-daemon如果它已经运行没有启动该程序。


如果进程仍未停止,则传递给的所有条件都start-stop-daemon --stop必须匹配。含义$pid必须是正在运行的进程,进程的 UID 必须匹配$user,并且进程名称 (arg0) 必须匹配$name
您可以通过执行以下操作来确定 arg0 的值ps h -p $pid -o comm