我創建了一個腳本來檢查是否有其他腳本正在運行。 如果腳本卡住某些情況下我想重新啓動腳本。在python中退出腳本時結束子流程
這裏是我的代碼
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
腳本strarts但其「ckeck」腳本內部執行。如果我停止執行檢查腳本,子進程結束。檢查腳本在crontab上,我想要在啓動腳本後堅持退出檢查腳本。 我怎麼能做到這一點? 感謝
不知道這是否是您的主要問題,但存儲在'statinfo'中的值是一個不會奇蹟般更新的元組。等待10秒後,您必須再次調用'os.stat()'。 – lenz
嗨@lenz,statinfo工作正常。我可以獲得文本的第一個大小,然後重新檢查它。這個腳本還在開發中。我稍後要對腳本進行一些更改,但這是我現在的主要問題 –
我只是說你的if語句永遠不會是真的,因爲即使if語句中的statinfo.st_size也不會改變,文件的大小發生變化。你也可以寫'如果假:' – lenz