简体   繁体   中英

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. 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. 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 & . This will separate your new process from the calling process, so it will survive the stopping of your python script.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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