考虑以下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循环也只会打印一次。有没有办法让脚本继续运行?
答案 0 :(得分:1)
我尝试过您第一个脚本,并且连续运行第二个脚本。尝试仅运行script_to_be_started.py
并确保其运行正常。第二个脚本一直运行到log语句的原因之一可能是时间丢失。
import time
因此,在打印日志消息后,由于缺少导入,第二个脚本将无提示地崩溃。
我假设您仅精简了日志记录内容,但是丢失的时间导入实际上是代码的一部分。