Compare commits

...

3 Commits

Author SHA1 Message Date
546ca9dfe2 feat: 添加定时任务后台运行和停止功能
新增service_runner.py用于后台运行定时任务,使用pythonw.exe实现无窗口运行
新增stop_scheduler.py用于停止后台定时任务进程
调整scheduler.py中的任务执行时间
2025-06-24 17:49:10 +08:00
84288fc66b refactor: 重构定时任务和消息发送逻辑
- 删除start_scheduler.bat脚本,改为直接运行scheduler.py
- 重构scheduler.py,添加日志记录和主函数封装
- 重构sendmsg.py,将逻辑封装到函数中并添加返回值
- 调整定时任务执行时间为每天17:30
2025-06-24 17:31:49 +08:00
699bac8008 feat: 更新定时任务时间并添加批量处理脚本
- 将定时任务时间从12:00改为14:39
- 新增start_scheduler.bat脚本用于管理进程
- 在config.py中添加messages_reciever配置项
- 优化sendmsg.py代码结构,使用配置项代替硬编码
2025-06-24 14:50:22 +08:00
5 changed files with 103 additions and 36 deletions

View File

@ -2,5 +2,6 @@ CONFIG = {
"app_id": "cli_a8de57f7e95f100c",
"app_secret": "jm2nkFnnSNVS3n076VOPfh8k7q0hEKVp",
"open_id": "ou_bd33a5f75dd2ccb005a21e4dd01f930c",
"file_path": "C:\\Users\\22251\\OneDrive\\桌面\\picture\\"
"file_path": "C:\\Users\\22251\\OneDrive\\桌面\\picture\\",
"messages_reciever": "File Transfer"
}

View File

@ -3,17 +3,36 @@ import time
import subprocess
import sys
import os
from datetime import datetime
def run_sendmsg():
"""执行sendmsg.py脚本"""
script_path = os.path.join(os.path.dirname(__file__), 'sendmsg.py')
subprocess.run([sys.executable, script_path])
try:
print(f"[{datetime.now()}] 开始执行每日消息发送任务...")
result = subprocess.run([sys.executable, script_path],
capture_output=True, text=True, cwd=os.path.dirname(__file__))
if result.returncode == 0:
print(f"[{datetime.now()}] 任务执行成功")
else:
print(f"[{datetime.now()}] 任务执行失败: {result.stderr}")
except Exception as e:
print(f"[{datetime.now()}] 执行出错: {str(e)}")
# 设置每天12点运行
schedule.every().day.at("12:00").do(run_sendmsg)
def main():
"""主函数 - 设置定时任务"""
# 每天17:30执行任务
schedule.every().day.at("17:40").do(run_sendmsg)
print("定时任务已启动将在每天12:00运行sendmsg.py")
print(f"[{datetime.now()}] 定时任务已启动将在每天17:30执行sendmsg.py")
print("按Ctrl+C停止程序")
# 保持程序运行
try:
while True:
schedule.run_pending()
time.sleep(1)
time.sleep(60) # 每分钟检查一次
except KeyboardInterrupt:
print(f"\n[{datetime.now()}] 定时任务已停止")
if __name__ == "__main__":
main()

View File

@ -7,30 +7,32 @@ import json
from config import CONFIG
import time
def send_daily_message():
"""发送每日消息的主函数"""
today = date.today()
formatted_date = today.strftime('%Y-%m-%d')
file_name = formatted_date + ".png"
file_path = CONFIG['file_path'] + file_name
try:
wx = WeChat()
except:
#运行send_openmsg.py
subprocess.run([sys.executable, 'send_openmsg.py'])
sys.exit(1)
return False
msg = 'hello, wxauto!'
who = 'File Transfer'
who = CONFIG['messages_reciever']
if os.path.isfile(file_path):
error_msg = ""
else:
error_msg = "没找到指定文件"
#运行send_filemsg.py
subprocess.run([sys.executable, 'send_filemsg.py'])
sys.exit(1)
print("找到了指定文件!")
wx.SendFiles(filepath=file_path, who="File Transfer")
wx.SendMsg(msg=msg, who=who)
return True
else:
print("没找到指定文件")
subprocess.run([sys.executable, 'send_filemsg.py'])
return False
# 如果直接运行此脚本,执行发送消息功能
if __name__ == "__main__":
send_daily_message()

20
service_runner.py Normal file
View File

@ -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()

25
stop_scheduler.py Normal file
View File

@ -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()