Debian 8 - Restart closed/stopped Python script process
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Debian 8 - Restart closed/stopped Python script process
Hello, I am running Debian 8 Jessie and I also run a Python script in a loop but sometimes it crashes and Python stops so I am wondering what I can use to monitor the running Python script and if stopped and not running anymore, the system will restart it. Thank you
Haven't really used them, I'm more familiar with services which I think are older, but this sounds exactly like what a cron job would do for you. It would run your script and if your script ended or failed, the structure of how you created the cron job would allow you to tell it to auto-restart, and in either case, for instance if it terminated normally or in error, you may wish to have that job rescheduled perpetually. This is what I understand cron to be for.
Hello, thanks for responding.
I can create a cron job but I'm only familiar with creating them to run a job every so many minutes, how do I set it to monitor it upon failure as I have the Python script looping indefinitely? Thanks
Actually, I was just thinking and my script just loops every 90 seconds so technically I can remove the loop from the Python script and have the cron job run the script every 90 seconds. What cron settings would I use to run it every 90 seconds? can I do this? : 1.5 * * * * /usr/bin/python script.py
EDIT: looks like I can't do 90 seconds so I would have to run it twice, every 3 minutes but have one of them sleep for 90 seconds;
Can I do this?
* * * * * sleep 100;/usr/bin/python script.py
So basically it will be told to run every minute but it will sleep 100 seconds then once the sleep is over, the minute will be over-do so will run immediately again then sleep again for 100 seconds and so on...
I believe cron runs once per minute.
If that's not frequent enough, I wouldn't suggest doing tricks like sleeping as that seems likely to break.
I'm pretty sure systemd is able to handle this but I've yet to find any clear documentation on how to do this (maybe others can provide this?)
Quote:
I also run a Python script in a loop but sometimes it crashes and Python stops
It might be wiser to address why it is crashing in the first place. Any errors? memory issues?
I will sometimes do this however:
Code:
while true;
do
/my/script.py
done
But that makes it hard (impossible) to stop it for good until removing that from cron and rebooting.
So I will also do the below. That way if I do touch /tmp/delete_if_i_exist and kill the python script, it won't restart
Code:
while [ ! -f /tmp/delete_if_i_exist ];
do
/my/script.py
done
This can easily be done in crontab
Code:
# /etc/cron
@reboot while true; do /my/script.py; done
@reboot while [ ! -f /tmp/delete_if_i_exist ]; do /my/script.py; done
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195
Rep:
Basically there are 2 options if the script stops:
The Pyhton process is killed and has disappeared
The Python process is still running but it doesn't do anything ('hung')
In the first case your detection is to run a "ps ax | grep name-of-your-script".
The second case is more difficult. The way I do it is to have my script to touch a file somewhere in /var/run and check the timestamp. In other applications I write logging data to a database. So I include the time stamp when I write and check the time stamp.
The next step is to create a start/stop script, like those used in /etc/init.d. In this start/stop script you do the check as described above and you avoid starting the script twice. So it is safe to start this script as many times as you want. It will however only create a new instance of the script if it is not running yet. It is an option to include you script in the scripts started for the runlevel at boot.
The you can call this script every 1-2 minutes from cron. I have set up these kind of mechanisms for years for real-time processes.
Real-time does not necessarily mean fast, but it means that there is interaction with the real world and the time is relevant. It is nice to try and find why the script stops, but that is not alway possible. If external events play a role, like network connections, remote database access, subprocesses which may not end it is possible that a script or programs gets stuck or crashes. For example if you pull the plug of a mounted drive, or this drive had a power failure. It might be possible to make your script bomb-proof. But it is easier to do the reasonable effort, and restart your script when stuck or terminated.
Oh, and don't forget to send yourself an e-mail when the script restarted. You want to know this. Or at the very least, log the event.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.