且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

批处理脚本调用可执行文件,并退出,如果它挂起

更新时间:2022-03-19 21:35:32

延迟扩展

@echo OFF &SETLOCAL ENABLEDELAYEDEXPANSION 
start calc.exe

REM loop 600 times, each loop being 3 seconds (30 minutes total)
FOR /L %%A IN (1,1,600) DO (

   REM find the running executable
   tasklist | find /I /C "calc.exe" > nul
   echo !ERRORLEVEL!

   Rem exit the script if no executable is found (i.e it has run successfully)
   if !ERRORLEVEL! eq 1 EXIT 

   Rem pause for 3 seconds
   ping 1.1.1.1 -n 1 -w 3000 > nul
)

REM kill executable if we haven't exited yet
taskkill /f /im calc.exe