简体   繁体   English

如何让 start-stop-daemon 将子进程 ID 写入 pid 文件?

[英]How can I get start-stop-daemon to write the child process ID to the pid file?

I'm writing an init.d script to start a program that I've written in C. The program follows a pretty standard template where it calls fork() to create a child process, and then the parent process terminates almost immediately.我正在编写一个 init.d 脚本来启动我用 C 编写的程序。该程序遵循一个非常标准的模板,它调用fork()创建一个子进程,然后父进程几乎立即终止。 The trouble I'm running into is that the PID file gets the parent process ID written to it, so later when I call "stop" it fails because the parent has long since ended and only the child is running.我遇到的问题是 PID 文件将父进程 ID 写入其中,所以稍后当我调用“停止”时它会失败,因为父进程早已结束并且只有子进程在运行。

Currently I have this command in my startup script:目前我的启动脚本中有这个命令:

start-stop-daemon --start --background -m --oknodo --pidfile "$PIDFILE" --exec "$SCRIPT" || return 2

I confess that I'm very new to all of this so I'm not even entirely sure what all those flags do.我承认我对这一切都很陌生,所以我什至不完全确定所有这些标志的作用。 This was mostly a copy/paste from various articles I stumbled across.这主要是我偶然发现的各种文章的复制/粘贴。 In any case, this would seem like a fairly common thing.无论如何,这似乎是一件相当普遍的事情。 How can I make it write the correct (ie child) process ID in the pid file?如何让它在pid文件中写入正确的(即子)进程ID?

Daemons need to create .pid file by own self.守护进程需要自己创建 .pid 文件。 Because of daemonizing procedure by it self, when you change your start pid.因为它自己守护进程,所以当你改变你的启动 pid 时。

Option --pidfile is for pid file which already created.选项--pidfile用于已创建的 pid 文件。 Also start-stop-daemon has option --make-pidfile , but it's useful only for program which not daemonize own self. start-stop-daemon 也有选项--make-pidfile ,但它只对不守护自己的程序有用。

-m, --make-pidfile ... Note: This feature may not work in all cases. -m, --make-pidfile ... 注意:此功能可能不适用于所有情况。 Most notably when the program being executed forks from its main process.最值得注意的是,当正在执行的程序从其主进程分叉时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM