如何捕获从init.d运行的linux守护进程的pid

时间:2021-06-28 19:24:36

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 . So that I can use the pid for termination. How can I do this?

但我不知道如何捕获守护进程的pid并将其存储在/var/run/.pid中的pid文件中。这样我就可以使用pid进行终止。我怎样才能做到这一点?

3 个解决方案

#1


8  

Try using start-stop-daemon(8) with the --pidfile argument in your init script. Have your program write its PID to a specified location (usually determined in a configuration file).

尝试在init脚本中使用带-pidfile参数的start-stop-daemon(8)。让程序将其PID写入指定位置(通常在配置文件中确定)。

What you have to look out for is stale PID files, for instance, if a lock file persisted across a reboot. That logic is best implemented in the init script itself, hence the --exec option to start-stop-daemon.

您需要注意的是陈旧的PID文件,例如,如果锁定文件在重新启动时持续存在。该逻辑最好在init脚本本身中实现,因此start-stop-daemon的--exec选项。

E.g, 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.

同样,除了一致地写入文件之外,所有这些逻辑都应该在应用程序之外处理。

#2


1  

If you know the port the program has open, use fuser command to determine the pid.

如果您知道程序已打开的端口,请使用fuser命令确定pid。

#3


1  

You could go about more than one way:

你可以采取不止一种方式:

  1. In your program use getpid to write it to a configurable file (perhaps looking in ENV)
  2. 在您的程序中使用getpid将其写入可配置文件(可能在ENV中查找)

  3. Use $! after starting the program (this doesn't work for me on archlinux though :-?)
  4. 使用$!在启动程序之后(虽然这对我在archlinux上不起作用: - ?)

  5. After starting the program, use pidof
  6. 启动程序后,使用pidof

#1


8  

Try using start-stop-daemon(8) with the --pidfile argument in your init script. Have your program write its PID to a specified location (usually determined in a configuration file).

尝试在init脚本中使用带-pidfile参数的start-stop-daemon(8)。让程序将其PID写入指定位置(通常在配置文件中确定)。

What you have to look out for is stale PID files, for instance, if a lock file persisted across a reboot. That logic is best implemented in the init script itself, hence the --exec option to start-stop-daemon.

您需要注意的是陈旧的PID文件,例如,如果锁定文件在重新启动时持续存在。该逻辑最好在init脚本本身中实现,因此start-stop-daemon的--exec选项。

E.g, 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.

同样,除了一致地写入文件之外,所有这些逻辑都应该在应用程序之外处理。

#2


1  

If you know the port the program has open, use fuser command to determine the pid.

如果您知道程序已打开的端口,请使用fuser命令确定pid。

#3


1  

You could go about more than one way:

你可以采取不止一种方式:

  1. In your program use getpid to write it to a configurable file (perhaps looking in ENV)
  2. 在您的程序中使用getpid将其写入可配置文件(可能在ENV中查找)

  3. Use $! after starting the program (this doesn't work for me on archlinux though :-?)
  4. 使用$!在启动程序之后(虽然这对我在archlinux上不起作用: - ?)

  5. After starting the program, use pidof
  6. 启动程序后,使用pidof