2

I wrote a twitter scraper that I want to run indefinitely on my server in Python/Django. How can I make sure it always runs? I understand I can make a cronjob to start it up.

However, sometimes the script errors because twitter's connection fails. How can I make something that checks if this script is still running every minute or so?

I could make a cronjob that start again every minute, but I wouldn't want that as if it is still running properly it doesn't need to be restarted.

Javaaaa
  • 3,788
  • 7
  • 43
  • 54
  • I guess you want to daemonize it. http://stackoverflow.com/questions/115974/what-would-be-the-simplest-way-to-daemonize-a-python-script-in-linux – Gazler Oct 08 '11 at 11:27
  • 2
    Your script should automatically recover from twitter connection problems. – Gleno Oct 08 '11 at 11:32

3 Answers3

2

Just ignore the exception, and start the script again.

while True:
    try:
        do_work()
    except Exception, e:
        print "Exception occurred, restarting.", e
        pass
Petr Viktorin
  • 65,510
  • 9
  • 81
  • 81
0

I use this code:

#!/bin/sh
while true; do
    /path/to/my/script;
done
0

Add your script to the crond Job .

For more :

http://www.cyberciti.biz/faq/how-do-i-add-jobs-to-cron-under-linux-or-unix-oses/

Jay D
  • 3,263
  • 4
  • 32
  • 48