0

I have a python script I use at work that checks the contents of a webpage and reports to me the changes. It used to run fine checking the site every 5 minutes and then our company switched some security software. The script will still run but will stop after an hour or so. Its not consistent, it will sometimes be a few hours but about an hour seems average. There are no errors raised that are reported in the shell. Is there a way to have this re-started automatically? The code is below, it used to just call the function and then a sleep command, but I added the for loop and the print line for debugging to see what time it is stopping.

import time
import datetime
import txtWip

while True:
    txtWip.main()
    for i in range(1, 300,100):
        current_time = time.strftime(r"%d.%m.%Y %H:%M:%S", time.localtime())
        print(current_time)
        time.sleep(100)
NoobLord
  • 23
  • 5
  • 1
    If the script runs successfully for first time and fails after a while, I would suggest you to configure a [cron job](http://www.unixgeeks.org/security/newbie/unix/cron-1.html). It is easy to manage. – Harish Talanki Jul 08 '16 at 20:36
  • 1
    I would strongly recommend that you use a scheduler for running scripts rather than have it sleep between executions. – Munir Jul 08 '16 at 20:36
  • ok, thanks. I will look into those. – NoobLord Jul 08 '16 at 20:45

1 Answers1

0

What you want is for your program to run as a daemon[1]: a background process that no longer responds to ordinary SIGKILL or even SIGHUP. You also want this daemon to restart itself on termination - effectively making it run forever.

Rather than write your own daemon script, it's far easier to do one of the following:

  • If on Linux, use Upstart.

    This is a replacement for the init.d daemon that supervises all processes while your machine running. It is capable of respawning a process in the event of an unexpected crash - see an example here, and a Python-specific example here. It is the gold standard for such tasks on this platform.

    For certain Ubuntu releases, systemd is the prodigal son and should be used instead.

    An alternative with an external bash script that doesn't require messing around with upstart is also a possibility.

  • If on Windows, use ReStartMe

  • If on Mac, install and configure runit appropriately


[1] The following is not cross-platform advice. You may or may not actually need to run this as a true daemon. A simple background job (i.e. invoked by appending an & when you run python <file_name>.py to the end) should be sufficient - this will quit when the terminal you ran it in quits, but you can get around this by using the Linux utility screen.

Community
  • 1
  • 1
Akshat Mahajan
  • 9,543
  • 4
  • 35
  • 44