systemd - 如何在 systemd 的看门狗停止服务之前运行特定程序

标签 systemd watchdog

我有一个由 systemd 运行的程序,其服务文件如下所示:

[Unit]
Description=...

[Service]
Type=notify
ExecStart=/usr/sbin/myprogram
WatchdogSec=1
KillMode=process
KillSignal=SIGTERM
Restart=always

它定期向看门狗发送相应的信号。有时,程序似乎挂起并被看门狗终止,然后重新启动。在看门狗终止它之前,我想通过执行命令或运行其他脚本(例如 run gdb -p <PID> --batch -ex 'thread apply all backtrace')从程序中捕获一些信息。我该怎么做?

最佳答案

添加 ExecStop=为您服务。

[Service]
ExecStart=....
ExecStop=/path/to/SomeOtherProgram
....

根据 systemd手册,如果 ExecStop选项可用,它将首先运行该选项,然后如果 ExecStart 下的进程在此之后仍然可用,它将运行 KillMode .

ExecStop= Commands to execute to stop the service started via ExecStart=. This argument takes multiple command lines, following the same scheme as described for ExecStart= above. Use of this setting is optional. After the commands configured in this option are run, it is implied that the service is stopped, and any processes remaining for it are terminated according to the KillMode= setting (see systemd.kill(5)). If this option is not specified, the process is terminated by sending the signal specified in KillSignal= when service stop is requested. Specifier and environment variable substitution is supported (including $MAINPID, see above).



编辑

如下面的评论所示,此解决方案可能不适用于 Watchdog service 中的选项文件。

关于systemd - 如何在 systemd 的看门狗停止服务之前运行特定程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52860164/

相关文章:

assembly - 软件看门狗定时器复位 MPC875 CPU 过快

python - 看门狗(osx)不通知远程网络更改

linux - 经过一定时间后释放字符驱动程序中的互斥量

c# - 阻塞函数调用的看门狗

objective-c - 在 Cocoa/Objective-C 中创建看门狗的原因和方法

shutdown - Systemd - 在 `ExecStop=` 期间运行实用程序 Docker 容器

node.js - 使用 systemd 激活套接字时没有数据

linux - 检测 D-Bus 上的禁止关闭

linux - Spring Boot 应用程序即服务 + VM 选项

systemd - 在Systemd中引用其他环境变量