0

Hopefully someone can tell me if I'm going at this the right way. I have a Python script which I want to launch at boot. That was easy enough by putting a file in /etc/init.d/myBootFile

I want to make sure the script will re-start if it crashes (ie. my bad code). It looks like I could configure a cron job to possibly run every minute to check and make sure its running?

Would this the right way to go? If not, could you please explain the best way to start a Python script and re-start it if it crashes.

For context, I'm using the Adafruit LCD (to act as simple input/output device). The inputs will allow me to enable the Adafruit Ultimate GPS. So I can bring GPS info back to the LCD. I did see the post/answer here which explains the gpsd daemon and its sort of what I'm after. I might not necessarily be using the GPS, so would it be fine to keep my above workflow and let the always running script handle the GPS (vs. the GPS as its own daemon)

1
  • This, sir, is an XY Question. The correct question to ask is "How do I start a task, in such a way that it will restart if it crashes?". Using cron is the wrong solution. Commented Jun 26, 2013 at 10:46

2 Answers 2

1

why do you need a cron script for that purpose, when something like this will do the job well enough:

while true :    # run forewer
    try :
        main()
    except :
        pass

on the other hand, if you insist, your python script may touch the file (or update log file, or do whatever you like, that leaves a time stamp in the file system), and your cron job may check this time stamp and restart the script if the difference between the time stamp and the current time is more than few minutes.

3
  • This is my recommendation as well, though the example given is simplistic. Read up on Python Exception handling for more information and options and examples. It is a very useful feature with countless applications.
    – Butters
    Commented Jun 20, 2013 at 16:26
  • Ya this approached didnt dawn on me, but it'll serve my purposes. Thanks
    – KHibma
    Commented Jun 21, 2013 at 1:04
  • What's your response to stackoverflow.com/questions/21553327/… then?
    – Ghanima
    Commented Apr 19, 2015 at 21:32
4

supervisord is such a monitoring daemon. It can launch processes when it starts (though this can be disabled, so processes are manually started). If a process crashes, it will be restarted.

As a bonus, it has a nice web interface: enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.