Trouble with keeping process launched by systemd service alive
0
votes
2
answers
1232
views
I am trying to to get my first
systemd
service working as desired. Here is what I have:
gateway-watchdog.service
:
# A systemd service for the Gateway Watchdog
[Unit]
Description=gateway-watchdog: Watchdog to ensure that Gateway (gateway.py) is running
[Service]
Type=simple
User=root
WorkingDirectory=/home/ubuntu/lora_gateway/
ExecStart=/home/ubuntu/lora_gateway/watchdog.sh
[Install]
WantedBy=multi-user.target
gateway-watchdog.timer
:
[Unit]
Description=gateway-watchdog: Timer to run the associated gateway-watchdog service
[Timer]
# Run service 1 minute after boot
OnBootSec=1min
# Run service 15 minutes after the last run
OnUnitActiveSec=30s
[Install]
WantedBy=timers.target
watchdog.sh
has these lines (contained in some if
statements):
#!/bin/bash
...
if [[condition]]; then
command="python3 gateway.py"
# Process needs to be run in the background with '&'. If it isn't, the systemd service will treat this script
# as still running and so not re-run it at the time specified by the timer service.
echo "2 $(date +'%F %T'): Running: $command" >> $LOG_FILE_PATH
eval "$command"
# Command must be run using eval. If it isn't, the ampersand at the end of the command is effectively ignored
exit
fi
What I need:
The service will run watchdog.sh
. If the if
conditions in the script are true, watchdog.sh
will launch python3 gateway.py
as a separate process and then exit, and the service will no longer be considered active so that it will run again 30 seconds later.
Through various combinations of different service Type
s (I've tried simple
, oneshot
, and forking
), using &
at the end of ExecStart
and/or my command line, and/or using nohup
, I am able to either launch and keep gateway.py
alive, or have the service run every 30 seconds, but not both.
When python3 gateway.py
stays alive, it always stays part of the service's CGroup
, so the service stays active (running)
.
How can I change this behavior to achieve what I'm trying to do? Any thoughts are greatly appreciated.
**EDIT**:
I have updated the file contents above to reflect changes made based on suggestions from U.
With the files as given above, sudo systemctl status gateway-watchdog.service
gives:
● gateway-watchdog.service - gateway-watchdog: Watchdog to ensure that Gateway (gateway.py) is running
Loaded: loaded (/etc/systemd/system/gateway-watchdog.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2022-01-13 01:45:25 UTC; 8s ago
TriggeredBy: ● gateway-watchdog.timer
Main PID: 30684 (watchdog.sh)
Tasks: 3 (limit: 4435)
CGroup: /system.slice/gateway-watchdog.service
├─30684 /bin/bash /home/ubuntu/lora_gateway/watchdog.sh
└─30696 python3 gateway.py
Jan 13 01:45:25 ubuntu systemd: Started gateway-watchdog: Watchdog to ensure that Gateway (gateway.py) is running.
Good: The gateway.py
process stays running.
Bad: The service does not run again 30 seconds later, because the previous run is still active.
If I put the &
back into command="python3 gateway.py &"
, sudo systemctl status gateway-watchdog.service
gives:
● gateway-watchdog.service - gateway-watchdog: Watchdog to ensure that Gateway (gateway.py) is running
Loaded: loaded (/etc/systemd/system/gateway-watchdog.service; enabled; vendor preset: enabled)
Active: inactive (dead) since Thu 2022-01-13 01:49:05 UTC; 6s ago
TriggeredBy: ● gateway-watchdog.timer
Process: 33724 ExecStart=/home/ubuntu/lora_gateway/watchdog.sh (code=exited, status=0/SUCCESS)
Main PID: 33724 (code=exited, status=0/SUCCESS)
Jan 13 01:49:05 ubuntu systemd: Started gateway-watchdog: Watchdog to ensure that Gateway (gateway.py) is running.
Jan 13 01:49:05 ubuntu systemd: gateway-watchdog.service: Succeeded.
Good: The service is inactive, so it does run again 30 seconds later.
Bad: The python3 gateway.py
process dies immediately.
Asked by nabelekt
(1 rep)
Jan 12, 2022, 04:31 AM
Last activity: Jul 29, 2025, 04:36 AM
Last activity: Jul 29, 2025, 04:36 AM