gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 12:27:12 34 4
gpt4 key购买 nike

我有一个由 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/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com