简体   繁体   English

如何从python中的另一个脚本启动脚本

[英]How to start a script from another script in python

I have created a script that checks if some other script is running 我创建了一个脚本来检查其他脚本是否正在运行

import os
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...")
        os.system("python selenium_nemo.py")
        print("SCRIPT STARTED")

If the script logs are increasing then we are OK, but if the script has stuck for some case i want to stop it and restart the script once more. 如果脚本日志增加,那么我们可以,但是如果在某些情况下脚本卡住了,我想停止它并再次重新启动脚本。 First i kill the script and then i'am executing os.system("python selenium_nemo.py") . 首先,我杀死脚本,然后执行os.system("python selenium_nemo.py") The script starts but it runs inside my main script. 该脚本已启动,但在我的主脚本中运行。 How i can i start the selenium_nemo.py on another proccess? 我如何在另一个过程中启动selenium_nemo.py 'The for loop is for later' “ for循环供以后使用”

Thanks 谢谢

You can use subprocess module for this. 您可以为此使用subprocess模块。 Check out this answer for more. 进一步了解此答案

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

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