我用while true编写了一个python脚本来执行捕获电子邮件附件的任务,但有时l发现它会在服务器上意外退出。
L在本地运行了4个多小时,没有任何问题,所以l可以确认代码是正确的。
那么,有没有一种机制可以在python意外退出时重启它,比如进程监控?我是linux的新手。
备注:l运行此python脚本,就像在shell脚本中运行python attachment.py &一样。
发布于 2017-11-22 16:12:56
虽然@triplee的评论肯定会起作用,但我担心会有一些事情发生,你会更好地理解。这就是脚本失败的原因。
如果没有进一步的细节,很难推测可能会发生什么。作为第一个调试工作,您可以尝试将整个主体包装在try ... except...块中的while True中,并使用except块来记录错误和/或程序状态。那是,
while True:
try:
... do some stuff...
except:
... log the exception, print to screen, record the values of key variables, etc.
continue这将允许您了解故障期间发生的情况,并编写更健壮的代码来处理该事件。
发布于 2017-11-22 16:06:48
您可以尝试使用Supervisor来管理您的流程。Supervisor能够配置进程退出状态的状态,并尝试重新启动它。
附加的是official document和example in Ubuntu
配置示例
[program:nodehook]
command=/usr/bin/node /srv/http.js
directory=/srv
autostart=true
autorestart=true
startretries=3
stderr_logfile=/var/log/webhook/nodehook.err.log
stdout_logfile=/var/log/webhook/nodehook.out.log
user=www-data
environment=SECRET_PASSPHRASE='this is secret',SECRET_TWO='another secrethttps://stackoverflow.com/questions/47429532
复制相似问题