由 init.d 启动的保持活动进程的标准或最佳方式

Adr*_*nez 15 startup debian daemon init init.d

我正在寻找一种标准方法或最佳实践来保持由init.dshell 脚本启动的守护进程。

或者更好的是,有没有办法让它直接从/etc/init.d

具体来说,我有一个名为 dtnd 的守护进程,它具有无限循环,用于查找意外结束的进程,如果有的话,守护进程会再次唤醒它们。此外,我使用 start-stop-daemon 工具来让进程从给定的系统用户运行。

我想从启动时运行这个 dtnd 守护进程。为了实现这种行为,我创建了一个 init.d 脚本,它使用 start、stop 和 status 命令“包装”dtnd 文件。

我有两个问题要解决:

  1. 有没有办法从 init.d shell 脚本中实现使某些进程保持活动状态。是标准/最佳实践吗?

  2. 建议使用无限循环保持进程存活?我想最好使用一些命令respawn来实现这一点。这是正确的?

我知道respawn命令的存在。我认为这就是我需要的,但我不明白之间的工作流程/etc/init.d//etc/init。谁能帮我?

请注意,我没有inittab文件既没有暴发户(我只被允许使用/etc/init/etc/init.dcron和系统工具start-stop-daemon。我的意思是,只有默认工具)

非常感谢您的参与!

Mic*_*ton 13

Debian will eventually have systemd, so this is the way to do it on a Linux system which uses systemd (and many do already; you might consider switching distributions).

Systemd can handle keeping the service alive for you automatically; no other tools are required. Simply make sure that Restart=always is set in the service file's [Service] section.

# vi /etc/systemd/system/dtnd.service

[Service]
Restart=always
#...everything else...
Run Code Online (Sandbox Code Playgroud)

Several other options are available as well, for more complex scenarios.

  • 虽然未来显示出更灵活的选择,但这是否会照顾到当前的环境/条件?与叉车分布更改/升级相比,安装工具似乎是阻力最小的路径。 (2认同)
  • 不要直接编辑使用更安全的 `systemctl edit myservice`,然后使用 `systemctl daemon-reload` 并重新启动 myservice。 (2认同)

Den*_*ker 5

您可以将其添加/etc/inittabrespawn

d1:2345:respawn:/path/to/your/first_daemon arg1 arg2
d2:2345:respawn:/path/to/your/second_daemon arg1 arg2
Run Code Online (Sandbox Code Playgroud)

这是一个肮脏的黑客行为,但我过去在旧的 sysv-init 系统上成功地使用过它。