[英]How to capture pid of a linux daemon run from init.d
I have started a service daemon , by running the binary(written in C++) through script file stored rc5.d . 我已经通过存储rc5.d的脚本文件运行二进制文件(用C ++编写)启动了一个服务守护进程。
But I am not sure how to capture the pid of the daemon process and store it in pid file in /var/run/.pid . 但我不知道如何捕获守护进程的pid并将其存储在/var/run/.pid中的pid文件中。 So that I can use the pid for termination. 这样我就可以使用pid进行终止。 How can I do this? 我怎样才能做到这一点?
Try using start-stop-daemon(8)
with the --pidfile
argument in your init script. 尝试在init脚本中使用带--pidfile
参数的start-stop-daemon(8)
。 Have your program write its PID to a specified location (usually determined in a configuration file). 让程序将其PID写入指定位置(通常在配置文件中确定)。
What you have to look out for is stale PID files, for instance, if a lock file persisted across a reboot. 您需要注意的是陈旧的PID文件,例如,如果锁定文件在重新启动时持续存在。 That logic is best implemented in the init script itself, hence the --exec
option to start-stop-daemon
. 该逻辑最好在init脚本本身中实现,因此start-stop-daemon
的--exec
选项。
Eg, if /var/run/foo.pid
is 1234
, and /proc/1234/exe
isn't your service, the lock file is stale and should be quietly removed, allowing the service to start normally. 例如,如果/var/run/foo.pid
是1234
,并且/proc/1234/exe
不是您的服务,则锁定文件是陈旧的,应该安静地删除,从而允许服务正常启动。
As far as your application goes, just make sure the location of the lockfile is configurable, and some means exists to tell the init script where to put it. 就您的应用程序而言,只需确保lockfile的位置是可配置的,并且存在一些方法来告诉init脚本将其放在何处。
For instance: (sample: /etc/default/foo) : 例如:(示例:/ etc / default / foo):
PIDFILE=/var/run/foo.pid
OTHEROPTION=foo
Then in /etc/init.d/foo : 然后在/etc/init.d/foo中:
[ -f /etc/default/foo ] && . /etc/default/foo
Again, other than writing to the file consistently, all of this logic should be handled outside of your application. 同样,除了一致地写入文件之外,所有这些逻辑都应该在应用程序之外处理。
如果您知道程序已打开的端口,请使用fuser
命令确定pid。
You could go about more than one way: 你可以采取不止一种方式:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.