systemd - Child 在 parent 退出时收到 SIGTERM

systemd - Child is receiving a SIGTERM on parent exit

环境:

我有一个 python 服务器分叉成 3 children.

对于parent:

对于child:

问题:

当我通过命令行 python <script.py> 启动脚本并将 SIGHUP 发送到 parent kill -1 <pid-of-parent> 时,它表现良好。 parent 退出,忙 children 仅在完成 ​​I/Os.

后退出

但是如果同一个脚本是用 systemd 脚本启动的,当我向 parent 发送 SIGHUP 时,仍然忙碌的 children 会收到一个 SIGTERM 并导致中断 I/O .

有什么 explanation/solutions 吗?是配置错误吗?

似乎您需要将系统服务的 KillMode 设置为 process

KillMode ... Specifies how processes of this unit shall be killed. ... If set to control-group, all remaining processes in the control group of this unit will be killed on unit stop ... If set to process, only the main process itself is killed ... Defaults to control-group

更多信息:https://www.freedesktop.org/software/systemd/man/systemd.kill.html#

另一种方法是在 subprocess.Popen 调用中将 creationflags 设置为 subprocess.CREATE_NEW_PROCESS_GROUP,如果您以这种方式生成进程。