简体   繁体   English

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

[英]subprocess ends when exiting the script in python

I have a created a script that checks if some other script is running. 我创建了一个脚本,用于检查是否正在运行其他脚本。 If the script has stuck for some case i want to restart the script. 如果脚本在某些情况下卡住了,我想重新启动脚本。

Here is my code 这是我的代码

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")

When i call the subprocess.call the script strarts but its executing inside the 'ckeck' script. 当我打电话的subprocess.call脚本strarts但其“ckeck”脚本内部执行。 If i stop executing the check-script the subprocess ends. 如果我停止执行检查脚本,则子过程结束。 The check script is on crontab and i want after starting the script that has stuck to exit the check script. 检查脚本位于crontab上,我想要在启动卡住的脚本后退出检查脚本。 How i can achieve this? 我怎样才能做到这一点? Thanks 谢谢

Instead of calling python /root/btree/selenium_nemo.py directly, you could call nohup python /root/btree/selenium_nemo.py & . 除了直接调用python /root/btree/selenium_nemo.py ,您还可以调用nohup python /root/btree/selenium_nemo.py & This will separate your new process from the calling process, so it will survive the stopping of your python script. 这会将您的新进程与调用进程分开,因此它将在停止python脚本后幸免于难。

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

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