自动更新脚本编写

This commit is contained in:
liuwei
2025-03-31 14:23:11 +08:00
parent 2bb92d5f6a
commit c3ef1ada05

View File

@@ -360,7 +360,8 @@ echo 再次确认无残留进程...
taskkill /F /IM python.exe /T 2>nul
taskkill /F /IM pythonw.exe /T 2>nul
echo 重新启动系统...
start "" "{bat_path}"
cd /d "{os.path.dirname(bat_path)}"
call "{bat_path}"
echo 等待系统启动和微信加载 ({wait_time}秒)...
timeout /t {wait_time} /nobreak > nul
echo 尝试登录微信...
@@ -369,32 +370,60 @@ echo 清理临时脚本...
(goto) 2>nul & del "%~f0" & exit
""")
# 启动临时脚本
# 确保临时脚本具有执行权限
os.chmod(temp_bat, 0o755)
print(f"启动临时脚本执行重启: {temp_bat}")
# 使用startupinfo隐藏cmd窗口防止重复窗口
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
startupinfo.wShowWindow = subprocess.SW_HIDE
# 使用subprocess.Popen而不是run这样不会等待它完成
subprocess.Popen(["cmd", "/c", temp_bat],
shell=True,
creationflags=subprocess.CREATE_NEW_CONSOLE,
startupinfo=startupinfo)
# 创建分离进程运行批处理
try:
# 方法1: 使用START命令启动独立进程
os.system(f'start "" "{temp_bat}"')
# 方法2: 作为备份机制也使用subprocess启动
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
subprocess.Popen(
temp_bat,
shell=True,
creationflags=subprocess.CREATE_NEW_PROCESS_GROUP | subprocess.DETACHED_PROCESS,
startupinfo=startupinfo
)
print("已通过两种方法启动临时脚本")
except Exception as e:
print(f"启动临时脚本时出错: {e}")
# 尝试直接回退到启动原始脚本
try:
print(f"尝试直接启动原始脚本: {bat_path}")
original_dir = os.getcwd()
os.chdir(os.path.dirname(bat_path))
subprocess.Popen(
f'cmd /c "{bat_path}"',
shell=True,
creationflags=subprocess.CREATE_NEW_PROCESS_GROUP | subprocess.DETACHED_PROCESS
)
os.chdir(original_dir)
except Exception as e2:
print(f"启动原始脚本时出错: {e2}")
# 等待脚本启动
print("已启动临时脚本等待3秒...")
time.sleep(3)
# 等待确保脚本启动
print("等待4秒确保启动脚本已开始执行...")
time.sleep(4)
# 直接调用全局终止方法确保干净退出
print("终止所有Python相关进程...")
os.system("taskkill /F /IM python.exe /T 2>nul")
# 多重终止保障
try:
# 确保使用系统命令终止所有Python进程
print("使用系统命令终止所有Python相关进程...")
os.system("taskkill /F /IM python.exe /T 2>nul")
os.system("taskkill /F /IM pythonw.exe /T 2>nul")
# 结束当前Python相关进程
print("使用psutil终止当前进程...")
terminate_processes()
except Exception as e:
print(f"终止进程时出错: {e}")
# 结束当前Python相关进程并退出
print("结束当前进程...")
terminate_processes()
# 确保进程退出
print("强制退出当前进程")
os._exit(0) # 使用os._exit确保立即退出
else: