syscharger
Posts: 19
Joined: Wed Aug 26, 2015 8:46 am

Restart python script from crontab when it crash or killed

Mon Dec 28, 2015 1:39 am

Added the following in crontab (sudo crontab -e) to check for running of python script (testpython.py) every 5 minutes
If script not running, execute the script (testpython.py)..
But unfortunately, it does not work. Any idea why? Did i miss something. Please advise. Thanks

Code: Select all

*/5 * * * * pgrep -f testpython.py || sudo python /home/pi/testpython.py

SonOfAMotherlessGoat
Posts: 690
Joined: Tue Jun 16, 2015 6:01 am

Re: Restart python script from crontab when it crash or kill

Mon Dec 28, 2015 1:47 am

Is the account this cron job runs under able to sudo without a password?
Account Inactive

syscharger
Posts: 19
Joined: Wed Aug 26, 2015 8:46 am

Re: Restart python script from crontab when it crash or kill

Mon Dec 28, 2015 1:57 am

SonOfAMotherlessGoat wrote:Is the account this cron job runs under able to sudo without a password?
Yes. able to.
i tried similar code below, no problem.

*/5 * * * * sudo python /home/pi/testpython.py

syscharger
Posts: 19
Joined: Wed Aug 26, 2015 8:46 am

Re: Restart python script from crontab when it crash or kill

Mon Dec 28, 2015 4:18 am

Is there alternative solution? If not using crontab.
Thanks

User avatar
DougieLawson
Posts: 39121
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Restart python script from crontab when it crash or kill

Mon Dec 28, 2015 10:20 am

syscharger wrote:Is there alternative solution? If not using crontab.
Thanks
Add a cron file in /etc/cron.d, if you have php5 installed there's an example file in there.

The simplest way to keep a python script running is

Code: Select all

#!/bin/bash

until (/home/pi/thingtokeepgettingrestartedwhenitdies.py);
do
sleep 30m # wait half an hour and give it another go.
done;
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

rayjoh
Posts: 27
Joined: Thu May 23, 2013 11:48 am

Re: Restart python script from crontab when it crash or kill

Mon Dec 28, 2015 11:58 am

It might be that sudo denies cronjobs. Add this line in /etc/sudoers, substitute "rj" with your username.

Code: Select all

Defaults:rj   !requiretty
-- Raymond

User avatar
alexeames
Forum Moderator
Forum Moderator
Posts: 2869
Joined: Sat Mar 03, 2012 11:57 am
Location: UK
Contact: Website

Re: Restart python script from crontab when it crash or kill

Mon Dec 28, 2015 12:20 pm

don't you need to include the full path to python in your cron job?

cron can be exceedingly fussy
Alex Eames RasPi.TV, RasP.iO

QuietZone
Posts: 89
Joined: Sat Dec 05, 2015 7:13 pm

Re: Restart python script from crontab when it crash or kill

Mon Dec 28, 2015 11:30 pm

alexeames wrote:don't you need to include the full path to python in your cron job?
Nope.
cron can be exceedingly fussy
Yes, but this isn't one of them.
"If you haven't got anything nice to say about anybody come sit next to me." — Alice Roosevelt Longworth

User avatar
jjex22
Posts: 153
Joined: Tue Feb 24, 2015 12:41 pm

Re: Restart python script from crontab when it crash or kill

Tue Dec 29, 2015 2:04 am

Just a side note, that It also occurs to me that python is pretty good at exceptions, and some defensive programming combined with exploring recent crashes may help more in the long term, so that a resurrection script becomes more of a backup?
I had the idea for a single board computer in every seat on airplanes... but it all just seemed a bit Pi in the Sky!

mosespi
Posts: 508
Joined: Mon May 12, 2014 3:35 pm
Location: 34,-118
Contact: Website

Re: Restart python script from crontab when it crash or kill

Tue Dec 29, 2015 5:25 am

As jjex22 mentioned you should try to make it more robust.. but a simple wrapper script using bash should ensure a restart if it does crash. Very simple code below, so simple its almost always going to work.. run it from rc.local if you wish, and keep the sleep in there just in case your script really hard crashes and refuses to start, it will keep it from restarting too fast and possibly using up most of your system resources.

And I missed reading Dougie's prior post.. this is a similar variant.

Regards,
-Moses

Code: Select all

#!/bin/sh

while [ 1 ]
do
        /home/pi/blablayourpythoncommand.py or whatever
        sleep 10
done
Power problems? MoPower UPS for the Pi
http://www.allspectrum.com/mopower/

jaizan
Posts: 2
Joined: Tue Oct 27, 2015 9:19 pm

Re: Restart python script from crontab when it crash or killed

Thu Oct 24, 2019 8:04 pm

Apologies for the dumb questions. I have PrivateEyePi running, but it drops out every few weeks. I'm trying to figure out how to make it restart if necessary.

1 Would something like the following work ?
2 Do I just put this in my CRON file ?
3 Is the example below just valid for something in the PI folder, so I need the correct path for restarter ?


#!/bin/sh

while [ 1 ]
do
/home/pi/restarter.py or whatever
sleep 10
done

User avatar
DougieLawson
Posts: 39121
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Restart python script from crontab when it crash or killed

Thu Oct 24, 2019 8:18 pm

Use a systemd service file. /etc/systemd/system/privatepi.service

Code: Select all

[Unit]
Description=Private Pie server

[Service]
ExecStart=/usr/bin/python3 /home/pi/testpython.py
User=pi
Restart=always
RestartSec=360
StartLimitInterval=720
StartLimitBurst=10
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=PrivEye

[Install]
WantedBy=multi-user.target
If it absolutely must run as root remove the User=pi line (it probably doesn't need sudo most thing don't).

Enable that for the next boot with sudo systemctl enable privatepi
Start it now with sudo systemctl start privatepi

Test the restart with
sudo systemctl status privatepi
sudo killall testpython.py
sudo systemctl status privatepi
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

Return to “General discussion”