I've some python scripts on a raspberry pi. They behave like a service, they run forever without exiting and process signals. They are meant to run in the background, writing some values to a MariaDB. They run unattended, without a logged in user.
However, sometimes they crash. I'm not sure why and to me it looks like it's really hard to find out what happens (random crashes, no pattern, no clue, sometimes they run for months, sometimes they crash every day).
As the signals this scripts deal with are not really important, i'd love to just:
- write a shell script that kills all running python scripts and starts them again
- use crontab to run this script twice a day, so if something crashes i don't lose too much data and i don't have to care about checking and restarting them
I've written the shell script "restart.sh" and executing it in bash works great:
#!/bin/bash PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin kill $(pgrep python3) sleep 2 nohup python3 script1.py & sleep 2 nohup python3 script2.py & sleep 2 nohup python3 script3.py & sleep 2 exit
I've also modified my crontab with
sudo crontab -e, containing this:
SHELL=/bin/bash PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin 0 9 * * * /home/pi/restart.sh 0 15 * * * /home/pi/restart.sh
Testing the crontab, i can see that the
restart.sh is executed but only the first command, the
kill command, works. All running python3 scripts are killed, but not started again.
I tried adding
SHELL, as that seems to be common problems, but that didn't change anything.
I also tried first to edit
crontab -e without
sudo, same problem.
What am i missing here? Why aren't the
nohup commands executed like they are if i run
./restart.sh manually in the terminal? What can i do to get this scripts started using crontab?