I have this python code which I need to run using systemd
and monitor if it is hung as well. The problem is, when I run the python script directly from systemd, it works fine. However when the python script is run from another shell script which is run from my systemd service, it says
sdping_py.service: Got notification message from PID 6828, but reception only permitted for main PID 6768
The problem seems to be the python script running as a child process of the shell script and systemd service expecting notifications from the shell script which is the main process for the service. How can I get around this? My application strictly needs to be run from a shell script.
Here's the python code I tried,
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
This is my service file
[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
And this is the shell file
#!/bin/bash
/usr/bin/python /home/teshanl/sdping/src/sdping_pub.py
EDIT:
Thanks to @georgexsh, adding exec
to the shell command solved my problem partially. My new question is how do I do the same with a roslaunch
command? A ROS node should be sending the heartbeat notification to the systemd
service. roslaunch
launches the nodes with seperate PIDs obviously
use exec
, to replace the bash process with python process:
exec /usr/bin/python ...
or set NotifyAccess
to all
, to allow the forked child python process sent sd message, see this thread.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With