47 lines
1.6 KiB
Python
47 lines
1.6 KiB
Python
import schedule
|
||
import time
|
||
import subprocess
|
||
import sys
|
||
import os
|
||
from datetime import datetime
|
||
import calendar
|
||
from config import CONFIG
|
||
|
||
def run_sendmsg():
|
||
"""执行sendmsg.py脚本,周末不执行"""
|
||
# 获取当前日期是星期几(0是星期一,6是星期日)
|
||
current_weekday = datetime.now().weekday()
|
||
|
||
# 如果是周末(周六或周日),不执行任务
|
||
if current_weekday >= 5: # 5是周六,6是周日
|
||
print(f"[{datetime.now()}] 今天是周末,不执行任务")
|
||
return
|
||
|
||
script_path = os.path.join(os.path.dirname(__file__), 'sendmsg.py')
|
||
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)}")
|
||
|
||
def main():
|
||
"""主函数 - 设置定时任务"""
|
||
schedule.every().day.at(CONFIG['checking_time']).do(run_sendmsg)
|
||
|
||
print(f"[{datetime.now()}] 定时任务已启动,将在每天07:55执行sendmsg.py")
|
||
print("按Ctrl+C停止程序")
|
||
|
||
try:
|
||
while True:
|
||
schedule.run_pending()
|
||
time.sleep(60) # 每分钟检查一次
|
||
except KeyboardInterrupt:
|
||
print(f"\n[{datetime.now()}] 定时任务已停止")
|
||
|
||
if __name__ == "__main__":
|
||
main() |