我创建了一个脚本来检查其他脚本是否正在运行。如果脚本在某些情况下卡住了,我想重新启动脚本。
这是我的密码
import os
import subprocess
from subprocess import call
import datetime
import time
from time import ctime
statinfo = os.stat('nemo_logs/nemo_log_file_' + time.strftime('%Y-%m-%d') + '.txt')
for i in range(1):
first_size = statinfo.st_size
time.sleep(10)
if statinfo.st_size > first_size:
print("SCRIPT IS RUNNING")
else:
print("SCRIPT IS NOT RUNNING. TRYING TO KILL THE SCRIPT...")
os.system("pkill -9 -f selenium_nemo.py")
print("SCRIPT KILLED. TRYING TO RESTART THE SCRIPT...")
subprocess.call("python /root/btree/selenium_nemo.py", shell=True)
print("SCRIPT STARTED")当我调用subprocess.call时,脚本会执行,但是它会在'ckeck‘脚本中执行。如果我停止执行检查脚本,子进程就会结束。check脚本位于crontab上,我希望在启动了坚持退出检查脚本的脚本之后。我怎么能做到这一点?谢谢
发布于 2017-04-24 09:51:16
您可以调用python /root/btree/selenium_nemo.py,而不是直接调用nohup python /root/btree/selenium_nemo.py &。这将将您的新进程与调用进程分离开来,因此它将在python脚本停止后存活下来。
https://stackoverflow.com/questions/43584545
复制相似问题