Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Batch: Shutdown PC if a Program isn't Running?

Is there a batch command that will check if a program is running and shutdown the PC if it isn't?

like image 788
curtisupshall Avatar asked Dec 05 '25 03:12

curtisupshall


2 Answers

Thats Possible, example looking for running Internet explorer:

@echo off

tasklist /fi "imagename eq iexplore.exe" | find /I "iexplore.exe" > nul
if errorlevel 1 goto SHUTDOWN

echo IE is already running
goto DONE

:SHUTDOWN
shutdown -s -f -t 0

goto DONE

:DONE
exit

EDIT

if you want to Loop this command use this:

@echo off
:loop
tasklist /fi "imagename eq iexplore.exe" | find /I "iexplore.exe" > nul
if errorlevel 1 goto SHUTDOWN

echo IE is already running
goto DONE

:SHUTDOWN
shutdown -s -f -t 0

goto DONE

:DONE
REM wait 5 sec
ping 127.0.0.1 -n 2 -w 5000 > NUL
goto loop
like image 198
Oli Avatar answered Dec 08 '25 08:12

Oli


Just for a one liner

for /l %%# in (1 0 2) do (tasklist|find /i "program.exe">nul ||(shutdown -s -f -t 0 & exit /b))

Create a loop starting in 1 until 2 with a step of 0, check if program.exe in tasklist output. If it fails run shutdown and exit.

like image 36
MC ND Avatar answered Dec 08 '25 07:12

MC ND



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!