当我每天晚上在 CI 进程中安装 Windows 服务时,我需要一些能够一直工作并且完全自动化的东西。由于某些原因,服务在卸载后总是被标记删除很长时间(5分钟或更长时间)。因此,我扩展了重新安装批处理脚本,以确保服务真正被删除(简化版本) :
REM Stop the service first
net stop My-Socket-Server
REM Same as installutil.exe, just implemented in the service
My.Socket.Server.exe /u
:loop1
REM Easy way to wait for 5 seconds
ping 192.0.2.2 -n 1 -w 5000 > nul
sc delete My-Socket-Server
echo %date% %time%: Trying to delete service.
if errorlevel 1072 goto :loop1
REM Just for output purposes, typically I get that the service does not exist
sc query My-Socket-Server
REM Installing the new service, same as installutil.exe but in code
My.Socket.Server.exe /i
REM Start the new service
net start My-Socket-Server