简体   繁体   English

更改计算机安全性后,Python脚本停止

[英]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. 我有一个在工作中使用的python脚本,用于检查网页的内容并向我报告更改。 It used to run fine checking the site every 5 minutes and then our company switched some security software. 过去,它每5分钟对站点进行一次精细检查,然后我们公司切换了一些安全软件。 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. 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. 下面的代码,它只是用来调用函数,然后是sleep命令,但是我添加了for循环和打印行以进行调试,以了解停止时间。

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. 您想要的是使程序作为守护程序运行[1]:一个不再响应普通SIGKILL甚至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 . 如果在Linux上,请使用Upstart

    This is a replacement for the init.d daemon that supervises all processes while your machine running. 这是init.d守护程序的替代,该守护程序在计算机运行时监督所有进程。 It is capable of respawning a process in the event of an unexpected crash - see an example here , and a Python-specific example here . 如果发生意外崩溃,它能够重新生成进程-请参见此处的示例此处特定Python的示例 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. 对于某些Ubuntu版本, systemd是浪子,应该改而使用。

    An alternative with an external bash script that doesn't require messing around with upstart is also a possibility. 与另一种外部bash脚本 ,不需要用乱搞upstart也是一种可能性。

  • If on Windows, use ReStartMe 如果在Windows上,请使用ReStartMe

  • If on Mac, install and configure runit appropriately 如果在Mac上,请适当安装和配置runit


[1] The following is not cross-platform advice . [1] 以下不是跨平台建议 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 . 一个简单的后台作业(即在运行python <file_name>.py通过在末尾附加&调用)就足够了-在您退出的终端退出时,这将退出,但是您可以使用Linux来解决此问题。实用程序screen

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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