From 546ca9dfe2d8e13a233b2d36bcfc344ac7fce978 Mon Sep 17 00:00:00 2001 From: teddy Date: Tue, 24 Jun 2025 17:49:10 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E5=AE=9A=E6=97=B6?= =?UTF-8?q?=E4=BB=BB=E5=8A=A1=E5=90=8E=E5=8F=B0=E8=BF=90=E8=A1=8C=E5=92=8C?= =?UTF-8?q?=E5=81=9C=E6=AD=A2=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 新增service_runner.py用于后台运行定时任务,使用pythonw.exe实现无窗口运行 新增stop_scheduler.py用于停止后台定时任务进程 调整scheduler.py中的任务执行时间 --- scheduler.py | 2 +- service_runner.py | 20 ++++++++++++++++++++ stop_scheduler.py | 25 +++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 service_runner.py create mode 100644 stop_scheduler.py diff --git a/scheduler.py b/scheduler.py index a2e1ba6..8bf22d4 100644 --- a/scheduler.py +++ b/scheduler.py @@ -22,7 +22,7 @@ def run_sendmsg(): def main(): """主函数 - 设置定时任务""" # 每天17:30执行任务 - schedule.every().day.at("17:27").do(run_sendmsg) + schedule.every().day.at("17:40").do(run_sendmsg) print(f"[{datetime.now()}] 定时任务已启动,将在每天17:30执行sendmsg.py") print("按Ctrl+C停止程序") diff --git a/service_runner.py b/service_runner.py new file mode 100644 index 0000000..3bbbdf6 --- /dev/null +++ b/service_runner.py @@ -0,0 +1,20 @@ +import subprocess +import sys +import os +from pathlib import Path + +def run_in_background(): + """在后台运行定时任务""" + script_path = Path(__file__).parent / "scheduler.py" + + # 使用pythonw.exe在后台运行(无窗口) + pythonw_path = sys.executable.replace('python.exe', 'pythonw.exe') + + subprocess.Popen([pythonw_path, str(script_path)], + cwd=str(Path(__file__).parent), + creationflags=subprocess.CREATE_NO_WINDOW) + + print("定时任务已在后台启动") + +if __name__ == "__main__": + run_in_background() \ No newline at end of file diff --git a/stop_scheduler.py b/stop_scheduler.py new file mode 100644 index 0000000..7f1dff6 --- /dev/null +++ b/stop_scheduler.py @@ -0,0 +1,25 @@ +import subprocess +import sys +import os + +def stop_scheduler(): + """停止定时任务进程""" + try: + # 查找并结束scheduler.py进程 + result = subprocess.run(['tasklist', '/FI', 'IMAGENAME eq pythonw.exe', '/FO', 'CSV'], + capture_output=True, text=True) + + if 'scheduler.py' in result.stdout: + # 结束包含scheduler.py的pythonw进程 + subprocess.run(['taskkill', '/F', '/IM', 'pythonw.exe'], check=True) + print("定时任务已停止") + else: + print("未找到运行中的定时任务") + + except subprocess.CalledProcessError as e: + print(f"停止进程时出错: {e}") + except Exception as e: + print(f"发生错误: {e}") + +if __name__ == "__main__": + stop_scheduler() \ No newline at end of file