在python中退出脚本时,子进程结束

时间:2017-04-24 09:33:49

标签: 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'脚本中执行时。如果我停止执行check-script,则子进程结束。检查脚本在crontab上,我想在启动已停止退出检查脚本的脚本之后。 我怎么能做到这一点? 感谢

1 个答案:

答案 0 :(得分:1)

您可以拨打String stopNum,stopName,stopLoc,studentName,studentId; JSONArray routedataArray = generalFunc.getJsonArray(objects,"stds"); for (int i = 0; i < routedataArray.length(); i++) { JSONObject jsonObject,studentobject; JSONArray studentArray; try { jsonObject = routedataArray.getJSONObject(i); stopNum=generalFunc.getStringFromJobject(jsonObject, "stopNum"); stopName=generalFunc.getStringFromJobject(jsonObject, "stopName"); stopLoc=generalFunc.getStringFromJobject(jsonObject, "loc"); studentArray=jsonObject.getJSONArray("students"); for(int j=0;j<studentArray.length();j++){ studentobject=studentArray.getJSONObject(j); studentName=generalFunc.getStringFromJobject(studentobject, "studentName"); studentId=generalFunc.getStringFromJobject(studentobject, "studentId"); mapchild=new HashMap<>(); mapchild.put("studentName",studentName); mapchild.put("studentId",studentId); } mapheader = new HashMap<>(); mapheader.put("stopNum",stopNum); mapheader.put("stopName",stopName); mapheader.put("loc",stopLoc); } catch (JSONException e) { e.printStackTrace(); } } ,而不是直接拨打python /root/btree/selenium_nemo.py。这将使您的新进程与调用进程分离,因此它将在您的python脚本停止后继续存在。