Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Systemd watchdog running program via a script

Tags:

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

like image 316
Teshan Shanuka J Avatar asked Oct 10 '18 06:10

Teshan Shanuka J


1 Answers

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.

like image 103
georgexsh Avatar answered Nov 30 '22 05:11

georgexsh