【问题标题】:Systemd watchdog running program via a scriptSystemd看门狗通过脚本运行程序
【发布时间】:2019-03-15 00:27:49
【问题描述】:

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

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

问题似乎是作为 shell 脚本的子进程运行的 python 脚本和 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,将exec 添加到shell 命令部分解决了我的问题。我的新问题是如何使用roslaunch 命令执行相同的操作? ROS 节点应该向systemd 服务发送心跳通知。 roslaunch 显然会启动具有单独 PID 的节点

【问题讨论】:

  • 如果你的shell脚本这么简单,你可以使用exec
  • 对不起,我没明白。你能解释一下吗?这是我在这里展示的一个示例程序。实际的 shell 脚本可能包含几行代码
  • 点赞exec /usr/bin/python ...
  • 成功了!谢谢。这将是我的问题的部分解决方案
  • 关于roslaunchroslaunch 有一个Python API 可能会对您有所帮助。有了它,就可以从 Python 脚本启动和监控节点。虽然文档不是很完整...

标签: python bash ros systemd watchdog


【解决方案1】:

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

exec /usr/bin/python ...

或将NotifyAccess设置为all,以允许分叉的子python进程发送sd消息,见this thread

【讨论】:

    猜你喜欢
    • 2021-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-21
    • 1970-01-01
    • 1970-01-01
    • 2011-12-04
    相关资源
    最近更新 更多