自动更新脚本编写

This commit is contained in:
liuwei
2025-03-31 14:13:04 +08:00
parent 17194c6966
commit 8477c4cb22

View File

@@ -30,40 +30,114 @@ def kill_process_by_name(process_name):
def kill_current_python_process(): def kill_current_python_process():
"""结束当前Python程序""" """结束当前Python程序及相关进程"""
current_pid = os.getpid() current_pid = os.getpid()
parent_pid = psutil.Process(current_pid).ppid() parent_pid = psutil.Process(current_pid).ppid()
print(f"当前Python进程PID: {current_pid}, 父进程PID: {parent_pid}") print(f"当前Python进程PID: {current_pid}, 父进程PID: {parent_pid}")
# 收集需要在退出前结束的进程 # 收集需要在退出前结束的进程
pids_to_kill = [] pids_to_kill = []
pid_to_process = {} # 存储PID到进程对象的映射
# 查找所有可能与当前进程相关的Python进程 # 查找所有Python进程
for proc in psutil.process_iter(['pid', 'name', 'cmdline']): for proc in psutil.process_iter(['pid', 'name', 'cmdline']):
try: try:
# 如果是Python进程并且不是当前进程 # 如果是Python进程并且不是当前进程
if ('python' in proc.info['name'].lower() and proc_pid = proc.info['pid']
proc.info['pid'] != current_pid and proc_name = proc.info['name'].lower()
proc.info['pid'] != parent_pid):
# 检查命令行参数,看是否与当前脚本相关 if ('python' in proc_name and proc_pid != current_pid):
cmdline = ' '.join(proc.info['cmdline']).lower() # 获取完整进程对象以便后续操作
script_name = os.path.basename(__file__).lower() full_proc = psutil.Process(proc_pid)
if script_name in cmdline or "win_click.py" in cmdline:
pids_to_kill.append(proc.info['pid']) # 检查是否为我们系统的进程(通过命令行或父进程关联)
print(f"将结束Python进程: {proc.info['name']} (PID: {proc.info['pid']})") is_related = False
# 1. 检查命令行是否与当前脚本相关
try:
cmdline = ' '.join(proc.info.get('cmdline', [])).lower()
script_name = os.path.basename(__file__).lower()
base_path = os.path.dirname(os.path.abspath(__file__))
project_name = os.path.basename(base_path).lower()
if any(x in cmdline for x in [script_name, "win_click.py", "robot.py", "main.py", project_name]):
is_related = True
except:
pass
# 2. 检查进程树关系
try:
if proc_pid == parent_pid: # 是当前进程的父进程
is_related = True
# 检查是否为相同进程树的进程
p = full_proc
while p.pid != 1: # 1是系统进程
if p.ppid() == current_pid or p.ppid() == parent_pid:
is_related = True
break
try:
p = psutil.Process(p.ppid())
except:
break
except:
pass
if is_related:
pids_to_kill.append(proc_pid)
pid_to_process[proc_pid] = full_proc
print(f"将结束Python相关进程: {proc.info['name']} (PID: {proc_pid})")
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess): except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
pass pass
# 注册程序退出时要杀死的进程 # 按进程优先级排序(子进程先结束)
def on_exit(): def get_process_depth(pid):
for pid in pids_to_kill: depth = 0
try: try:
os.kill(pid, signal.SIGTERM) p = psutil.Process(pid)
except: while p.pid != 1: # 1是系统进程
pass depth += 1
try:
p = psutil.Process(p.ppid())
except:
break
except:
pass
return depth
# 返回退出函数,由调用者决定何时执行 # 将进程按深度排序(深度大的先终止)
return on_exit pids_sorted = sorted(pids_to_kill, key=get_process_depth, reverse=True)
# 终止进程函数
def terminate_processes():
# 首先尝试温和终止
try:
for pid in pids_sorted:
if pid != current_pid and pid != parent_pid:
try:
proc = pid_to_process.get(pid) or psutil.Process(pid)
print(f"温和终止进程: {proc.name()} (PID: {pid})")
proc.terminate()
except Exception as e:
print(f"温和终止进程 {pid} 时出错: {e}")
# 等待进程终止
gone, alive = psutil.wait_procs([pid_to_process.get(pid) or psutil.Process(pid)
for pid in pids_sorted if pid != current_pid and pid != parent_pid],
timeout=3)
# 强制终止仍在运行的进程
for proc in alive:
try:
print(f"强制终止进程: {proc.name()} (PID: {proc.pid})")
proc.kill()
except Exception as e:
print(f"强制终止进程 {proc.pid} 时出错: {e}")
except Exception as e:
print(f"终止进程时发生错误: {e}")
# 返回处理函数
return terminate_processes
def find_wechat_window(): def find_wechat_window():
@@ -215,11 +289,7 @@ def find_and_click_wechat_login(max_retries=3, retry_interval=5):
def update_system(wait_time=15): def update_system(wait_time=15):
"""完整的系统更新流程 """完整的系统更新流程"""
Args:
wait_time: 等待微信启动的秒数默认15秒
"""
print("=== 开始系统更新流程 ===") print("=== 开始系统更新流程 ===")
# 步骤1: 结束微信进程 # 步骤1: 结束微信进程
@@ -229,8 +299,8 @@ def update_system(wait_time=15):
else: else:
print("未找到运行中的微信进程") print("未找到运行中的微信进程")
# 步骤2: 注册结束当前Python进程的函数 # 步骤2: 获取终止当前Python进程的函数
exit_handler = kill_current_python_process() terminate_processes = kill_current_python_process()
# 步骤3: 准备重启系统 # 步骤3: 准备重启系统
print("步骤3: 准备重启系统...") print("步骤3: 准备重启系统...")
@@ -266,15 +336,15 @@ exit
print("启动临时脚本执行重启...") print("启动临时脚本执行重启...")
# 使用subprocess.Popen而不是run这样不会等待它完成 # 使用subprocess.Popen而不是run这样不会等待它完成
subprocess.Popen(["cmd", "/c", temp_bat], subprocess.Popen(["cmd", "/c", temp_bat],
shell=True, shell=True,
creationflags=subprocess.CREATE_NEW_CONSOLE) creationflags=subprocess.CREATE_NEW_CONSOLE)
# 等待一小段时间确保脚本开始运行 # 等待一小段时间确保脚本开始运行
time.sleep(1) time.sleep(1)
# 结束当前Python相关进程并退出 # 结束当前Python相关进程并退出
print("结束当前进程...") print("结束当前进程...")
exit_handler() terminate_processes()
sys.exit(0) sys.exit(0)
else: else:
print(f"未找到启动脚本 bot_start.bat无法自动重启系统") print(f"未找到启动脚本 bot_start.bat无法自动重启系统")