import schedule import time from core.utils import get_current_date_time import core.logger as logging import subprocess import os import sys logger = logging.logger # 定义要执行的任务 def run_script(): start_time = time.time() logger.info(f"Executing script at: {get_current_date_time()}") output_file = r'./output/auto_schedule.txt' with open(output_file, 'a') as f: f.write(f"Task ran at {get_current_date_time()}\n") current_dir = os.getcwd() python_path = sys.executable if current_dir.endswith('crypto_quant'): script_path = r'./monitor_schedule.py' elif current_dir.endswith(r'python_projects'): script_path = f'{current_dir}/crypto_quant/monitor_schedule.py' else: script_path = f'{current_dir}/monitor_schedule.py' subprocess.run([python_path, script_path]) end_time = time.time() logger.info(f"Script execution time: {end_time - start_time} seconds") # 设置每20秒运行一次 schedule.every(20).seconds.do(run_script) # 保持程序运行并检查调度 logger.info("Scheduler started. Press Ctrl+C to stop.") while True: schedule.run_pending() time.sleep(1)