简体   繁体   English

Windows上的Python中的自动重启脚本

[英]Self-Restarting Script in Python on Windows

I have a script that is communicating with an outside server that sometimes throws out errors and mucks up my program. 我有一个与外部服务器通信的脚本,有时会抛出错误并弄乱我的程序。 I have tried to workaround it, but it seems like it keeps throwing errors unless I manually restart the program. 我试图解决该问题,但是除非我手动重新启动该程序,否则它似乎总是抛出错误。

Is there any way on Windows to have a python script restart itself on error? Windows上是否有办法让python脚本在错误时自行重启? I see this ox.exec whenever I search these types of things and just can't seem to figure out making it work 每当我搜索这些类型的东西时,我都会看到这个ox.exec,但似乎无法弄清楚它是否起作用

In my main code: 
except:
    print "Unexpected error:", sys.exc_info()[0]
    logout()



def logout():
gv.logout()
time.sleep(20)
login()



def login():
try:
    gv.login(settings.user,settings.passw)
    textcheck()
except:
    print "Unexpected error:", sys.exc_info()[0]
    time.sleep(30)
    login()

you can run another program to check if it is alive, eg python program update a heartbeat file every minutes, and your guarder process check if the files last modified time to see the other end is alive. 您可以运行另一个程序来检查它是否还活着,例如python程序每分钟更新一次心跳文件,而您的保护程序则检查文件上次修改的时间是否看到另一端还活着。 you can also use WMI ,just write a query 您也可以使用WMI ,只需编写查询

select * from win32_process where xxx 

to check if python script is running. 检查python脚本是否正在运行。

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

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