Systemd watchdog 运行 通过脚本程序

Systemd watchdog running program via a script

我有这个 python 代码,我需要使用 systemd 运行 并监控它是否挂起。问题是,当我直接从 systemd 运行 python 脚本时,它工作正常。然而,当 python 脚本是来自另一个 shell 脚本的 运行 时,它是来自我的 systemd 服务的 运行 ,它说

sdping_py.service: Got notification message from PID 6828, but reception only permitted for main PID 6768

问题似乎是 python 脚本 运行ning 作为 shell 脚本的子进程和 systemd 服务期望来自 shell 脚本的通知服务的主要进程。我该如何解决这个问题?我的应用程序严格需要 运行 来自 shell 脚本。

这是我试过的 python 代码,

import sdnotify, time

n = sdnotify.SystemdNotifier()
print("Gonna start")
time.sleep(2)
print("Started!")

n.notify("READY=1")
i=0
while True:
    print(i)
    time.sleep(1)
    n.notify("WATCHDOG=1")
    i+=1

这是我的服务文件

[Unit]
Description=Test watchdog Demo process
DefaultDependencies=false
Requires=basic.target

[Service]
Type=notify
WatchdogSec=2
ExecStart=/home/teshanl/sdping/scripts/sdping_py.sh
#ExecStart=/usr/bin/python /home/teshanl/sdping/src/sdping_pub.py
StartLimitInterval=5min
StartLimitBurst=5
#StartLimitAction=reboot
Restart=always

这是 shell 文件

#!/bin/bash

/usr/bin/python /home/teshanl/sdping/src/sdping_pub.py

编辑:

感谢@georgexsh,在 shell 命令中添加 exec 部分解决了我的问题。我的新问题是如何使用 roslaunch 命令执行相同的操作? ROS 节点应该向 systemd 服务发送心跳通知。 roslaunch 显然用单独的 PID 启动节点

使用exec,将bash进程替换为python进程:

exec /usr/bin/python ...

或设置NotifyAccess to all, to allow the forked child python process sent sd message, see this thread.