简体   繁体   中英

Python script stopping after changes to computer security

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)

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 (ie 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM