feat: 改进定时任务管理并更新配置
- 在service_runner.py中添加进程ID记录功能 - 增强stop_scheduler.py以同时处理scheduler.py和sendmsg.py进程 - 修改sendmsg.py增加重试机制 - 更新配置文件路径和执行时间
This commit is contained in:
parent
7233700539
commit
aaf5f59b59
1
@AutomationLog.txt
Normal file
1
@AutomationLog.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
2025-08-14 10:00:11.042 sessionbox.py[249] _click -> Can not move cursor. ListItemControl's BoundingRectangle is (0,0,0,0)[0x0]. SearchProperties: {ControlType: ListItemControl}
|
||||||
@ -2,9 +2,9 @@ CONFIG = {
|
|||||||
"app_id": "cli_a8d64a7be63a500e",
|
"app_id": "cli_a8d64a7be63a500e",
|
||||||
"app_secret": "mcK8aTiq0CLtkzGs2aTZpcnom5J4o6yB",
|
"app_secret": "mcK8aTiq0CLtkzGs2aTZpcnom5J4o6yB",
|
||||||
"open_id": ["ou_c6466a45623096cf7a34d94fe30c6c73", "ou_3b94d0caf83dbced8b0e26af4852a281"],
|
"open_id": ["ou_c6466a45623096cf7a34d94fe30c6c73", "ou_3b94d0caf83dbced8b0e26af4852a281"],
|
||||||
"file_path": "C:\\Users\\22251\\OneDrive\\桌面\\picture\\",
|
"file_path": "F:\\",
|
||||||
"messages_reciever": "文件传输",
|
"messages_reciever": "文件传输助手",
|
||||||
"checking_time": "17:15",
|
"checking_time": "15:24",
|
||||||
"sending_time": "17:17",
|
"sending_time": "15:26",
|
||||||
"message": "新的一天,从华智长盈每日AI新闻开始!让我们一起看看今天AI圈有啥新鲜事!"
|
"message": "新的一天,从华智长盈每日AI新闻开始!让我们一起看看今天AI圈有啥新鲜事!"
|
||||||
}
|
}
|
||||||
@ -22,10 +22,10 @@ def run_sendmsg():
|
|||||||
|
|
||||||
def main():
|
def main():
|
||||||
"""主函数 - 设置定时任务"""
|
"""主函数 - 设置定时任务"""
|
||||||
# 每天17:30执行任务
|
# 每天07:55执行任务
|
||||||
schedule.every().day.at(CONFIG['checking_time']).do(run_sendmsg)
|
schedule.every().day.at(CONFIG['checking_time']).do(run_sendmsg)
|
||||||
|
|
||||||
print(f"[{datetime.now()}] 定时任务已启动,将在每天17:30执行sendmsg.py")
|
print(f"[{datetime.now()}] 定时任务已启动,将在每天07:55执行sendmsg.py")
|
||||||
print("按Ctrl+C停止程序")
|
print("按Ctrl+C停止程序")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|||||||
27
sendmsg.py
27
sendmsg.py
@ -28,18 +28,28 @@ def wait_until_time():
|
|||||||
|
|
||||||
time.sleep(wait_seconds)
|
time.sleep(wait_seconds)
|
||||||
|
|
||||||
def send_daily_message():
|
def send_daily_message(count):
|
||||||
"""发送每日消息的主函数"""
|
"""发送每日消息的主函数"""
|
||||||
today = date.today()
|
today = date.today()
|
||||||
formatted_date = today.strftime('%Y-%m-%d')
|
formatted_date = today.strftime('%Y-%m-%d')
|
||||||
file_name = formatted_date + ".png"
|
file_name = formatted_date + ".jpg"
|
||||||
file_path = CONFIG['file_path'] + file_name
|
file_path = CONFIG['file_path'] + file_name
|
||||||
|
num = count
|
||||||
|
if num >= 4:
|
||||||
|
print("已尝试4次,程序将退出")
|
||||||
|
sys.exit(0) # 直接终止程序
|
||||||
|
|
||||||
try:
|
try:
|
||||||
wx = WeChat()
|
wx = WeChat()
|
||||||
except:
|
except:
|
||||||
subprocess.run([sys.executable, 'send_openmsg.py'])
|
subprocess.run([sys.executable, 'send_openmsg.py'])
|
||||||
return False
|
if num<4:
|
||||||
|
time.sleep(60)
|
||||||
|
num += 1
|
||||||
|
send_daily_message(num)
|
||||||
|
else:
|
||||||
|
print("已尝试4次,程序将退出")
|
||||||
|
sys.exit(0) # 直接终止程序
|
||||||
|
|
||||||
msg = CONFIG['message']
|
msg = CONFIG['message']
|
||||||
who = CONFIG['messages_reciever']
|
who = CONFIG['messages_reciever']
|
||||||
@ -58,8 +68,15 @@ def send_daily_message():
|
|||||||
else:
|
else:
|
||||||
print("没找到指定文件")
|
print("没找到指定文件")
|
||||||
subprocess.run([sys.executable, 'send_filemsg.py'])
|
subprocess.run([sys.executable, 'send_filemsg.py'])
|
||||||
return False
|
if num<4:
|
||||||
|
time.sleep(60)
|
||||||
|
num += 1
|
||||||
|
send_daily_message(num)
|
||||||
|
else:
|
||||||
|
print("已尝试4次,程序将退出")
|
||||||
|
sys.exit(0) # 直接终止程序
|
||||||
|
|
||||||
# 如果直接运行此脚本,执行发送消息功能
|
# 如果直接运行此脚本,执行发送消息功能
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
send_daily_message()
|
count = 0
|
||||||
|
send_daily_message(count)
|
||||||
|
|||||||
@ -6,15 +6,21 @@ from pathlib import Path
|
|||||||
def run_in_background():
|
def run_in_background():
|
||||||
"""在后台运行定时任务"""
|
"""在后台运行定时任务"""
|
||||||
script_path = Path(__file__).parent / "scheduler.py"
|
script_path = Path(__file__).parent / "scheduler.py"
|
||||||
|
pid_file = Path(__file__).parent / "scheduler_pid.txt"
|
||||||
|
|
||||||
# 使用pythonw.exe在后台运行(无窗口)
|
# 使用pythonw.exe在后台运行(无窗口)
|
||||||
pythonw_path = sys.executable.replace('python.exe', 'pythonw.exe')
|
pythonw_path = sys.executable.replace('python.exe', 'pythonw.exe')
|
||||||
|
|
||||||
subprocess.Popen([pythonw_path, str(script_path)],
|
process = subprocess.Popen([pythonw_path, str(script_path)],
|
||||||
cwd=str(Path(__file__).parent),
|
cwd=str(Path(__file__).parent),
|
||||||
creationflags=subprocess.CREATE_NO_WINDOW)
|
creationflags=subprocess.CREATE_NO_WINDOW)
|
||||||
|
|
||||||
print("定时任务已在后台启动")
|
# 保存进程ID到文件
|
||||||
|
with open(pid_file, 'w') as f:
|
||||||
|
f.write(str(process.pid))
|
||||||
|
|
||||||
|
print(f"定时任务已在后台启动,进程ID: {process.pid}")
|
||||||
|
print(f"进程ID已保存到: {pid_file}")
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
run_in_background()
|
run_in_background()
|
||||||
@ -5,9 +5,35 @@ import psutil
|
|||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
def stop_scheduler():
|
def stop_scheduler():
|
||||||
"""停止定时任务进程"""
|
"""停止定时任务进程和相关的sendmsg.py进程"""
|
||||||
stopped_count = 0
|
stopped_count = 0
|
||||||
|
|
||||||
|
# 方法0: 从PID文件中读取进程ID
|
||||||
|
pid_file = Path(__file__).parent / "scheduler_pid.txt"
|
||||||
|
if pid_file.exists():
|
||||||
|
try:
|
||||||
|
with open(pid_file, 'r') as f:
|
||||||
|
pid = int(f.read().strip())
|
||||||
|
|
||||||
|
try:
|
||||||
|
process = psutil.Process(pid)
|
||||||
|
print(f"从PID文件中找到进程: PID={pid}")
|
||||||
|
process.terminate()
|
||||||
|
process.wait(timeout=5) # 等待进程正常结束
|
||||||
|
print(f"已终止进程 PID={pid}")
|
||||||
|
stopped_count += 1
|
||||||
|
# 删除PID文件
|
||||||
|
pid_file.unlink()
|
||||||
|
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
|
||||||
|
print(f"PID文件中的进程 {pid} 已不存在或无法访问")
|
||||||
|
# 删除过期的PID文件
|
||||||
|
pid_file.unlink()
|
||||||
|
except Exception as e:
|
||||||
|
print(f"从PID文件读取进程ID失败: {str(e)}")
|
||||||
|
else:
|
||||||
|
print("未找到PID文件,尝试其他方法查找进程...")
|
||||||
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# 方法1: 使用psutil精确查找进程
|
# 方法1: 使用psutil精确查找进程
|
||||||
current_dir = str(Path(__file__).parent)
|
current_dir = str(Path(__file__).parent)
|
||||||
@ -17,8 +43,9 @@ def stop_scheduler():
|
|||||||
if proc.info['name'] in ['python.exe', 'pythonw.exe']:
|
if proc.info['name'] in ['python.exe', 'pythonw.exe']:
|
||||||
cmdline = proc.info['cmdline']
|
cmdline = proc.info['cmdline']
|
||||||
if cmdline and len(cmdline) > 1:
|
if cmdline and len(cmdline) > 1:
|
||||||
# 检查是否是我们的scheduler.py进程
|
# 检查是否是我们的scheduler.py或sendmsg.py进程
|
||||||
if 'scheduler.py' in ' '.join(cmdline) and current_dir in ' '.join(cmdline):
|
cmdline_str = ' '.join(cmdline)
|
||||||
|
if ('scheduler.py' in cmdline_str or 'sendmsg.py' in cmdline_str) and current_dir in cmdline_str:
|
||||||
print(f"找到进程: PID={proc.info['pid']}, 命令行={' '.join(cmdline)}")
|
print(f"找到进程: PID={proc.info['pid']}, 命令行={' '.join(cmdline)}")
|
||||||
proc.terminate()
|
proc.terminate()
|
||||||
proc.wait(timeout=5) # 等待进程正常结束
|
proc.wait(timeout=5) # 等待进程正常结束
|
||||||
@ -39,7 +66,7 @@ def stop_scheduler():
|
|||||||
current_dir = str(Path(__file__).parent)
|
current_dir = str(Path(__file__).parent)
|
||||||
|
|
||||||
for line in lines[1:]: # 跳过标题行
|
for line in lines[1:]: # 跳过标题行
|
||||||
if line.strip() and 'scheduler.py' in line and current_dir in line:
|
if line.strip() and ('scheduler.py' in line or 'sendmsg.py' in line) and current_dir in line:
|
||||||
# 提取PID
|
# 提取PID
|
||||||
parts = line.split(',')
|
parts = line.split(',')
|
||||||
if len(parts) >= 2:
|
if len(parts) >= 2:
|
||||||
@ -69,9 +96,9 @@ def stop_scheduler():
|
|||||||
print(f"最后备用方案失败: {e}")
|
print(f"最后备用方案失败: {e}")
|
||||||
|
|
||||||
if stopped_count > 0:
|
if stopped_count > 0:
|
||||||
print(f"成功停止了 {stopped_count} 个定时任务进程")
|
print(f"成功停止了 {stopped_count} 个定时任务相关进程(scheduler.py或sendmsg.py)")
|
||||||
else:
|
else:
|
||||||
print("未找到运行中的定时任务进程")
|
print("未找到运行中的定时任务相关进程(scheduler.py或sendmsg.py)")
|
||||||
|
|
||||||
# 额外检查:查看是否还有相关进程
|
# 额外检查:查看是否还有相关进程
|
||||||
print("\n检查剩余进程...")
|
print("\n检查剩余进程...")
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user