net stop XK.Service echo "已停止服务,开始更新!" set /a t = 3 echo %t% :loop ping -n 10 127.1 >nul set /a t = %t% -1 echo 开始拷贝 xcopy D:codecodeservice_hangfire D:webOP.Service /y /e /i /q if %errorlevel% equ 0 ( echo "更新成功!" ) else ( if %t% equ 0 ( echo 更新超时 ) else ( goto loop ) ) echo "更新完成!" if %t% leq 0 echo 更新超时 net start XK.Service echo "已启动服务!"
服务停止后文件还在占用,没法直接copy,所以就等待30秒,然后如果copy失败重新尝试,每一次都比上一次多等待30秒
坑1、%time% 是内置变量
坑2、if %time% lss 250 if %errorlevel% equ 4 goto loop 两个if 中间不能换行,否则报错命令语法不正确。因为 换行也是命令。