[英]Is there a way to make python subprocess not terminate after running through the script once?
考慮以下2個文件
script_to_start_other_script.py
import schedule
import time
import subprocess
def run_again():
subprocess.call(["bash", "-c", "" + " nohup python script_to_be_started.py > /dev/null 2>&1&"])
if __name__== "__main__":
schedule.every(5).seconds.do(run_again)
while True:
schedule.run_pending()
time.sleep(1)
pass
script_to_be_started.py
import logging
from logging.handlers import TimedRotatingFileHandler
# Init logger
logger = logging.getLogger('test_log')
hdlr = logging.handlers.TimedRotatingFileHandler('./test_log.log', when='h', interval=10)
formatter = logging.Formatter('%(asctime)s %(levelname)s : %(message)s')
hdlr.setFormatter(formatter)
logger.addHandler(hdlr)
logger.setLevel(logging.INFO)
logger.info('Beginning of test_log.py')
import schedule
def run_again():
logger.info('I am being called')
if __name__== "__main__":
schedule.every(5).seconds.do(run_again)
while True:
logger.info('how many time am I being called')
schedule.run_pending()
time.sleep(1)
pass
每當我運行script_to_start_other_script.py
, script_to_be_started.py
將只運行整個腳本一次
logger.info('how many time am I being called')
即使有while循環,也只會打印一次。 有沒有辦法讓腳本繼續運行?
我嘗試過您第一個腳本,並且連續運行第二個腳本。 嘗試僅運行script_to_be_started.py
並確保其運行正常。 第二個腳本一直運行到log語句的原因之一可能是時間丟失。
import time
因此,在打印日志消息后,由於缺少導入,第二個腳本將無提示地崩潰。
我假設您只刪除了日志記錄內容,但是丟失的時間導入實際上是代碼的一部分。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.