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)

KHibma
  • 280
  • 2
  • 6
  • 16

2 Answers2

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

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.

lenik
  • 11,541
  • 1
  • 30
  • 37