Page 1 of 1
Detect when a batch file crashes or closes unexpectedly?
Posted: 27 Oct 2020 05:24
by rasil
I have a little program called
watchdog.bat that is ran silently in the background. It monitors another batch file called
ape.bat. When
ape.bat encounters a crash
watchdog.bat should run a batch file called
error.bat to inform the user that
ape.bat has crashed!
watchdog.bat should still be running in the background to detect more crashes.
I tired this code below.
Code: Select all
@echo off
cls
echo App started.
call "ape.bat"
cls
echo WARNING: App closed or crashed!
pause
exit
But the problem is that all of this is happening in the same window and the user can easily just click the cross on the top to terminate the whole program. I want to split this up into 2 windows and only one of them the user can see so even if the user clicks the cross on the top right hand corner then watchdog.bat should kick in and run the batch file called error.bat.
Thanks for reading and have a good day!

Re: Detect when a batch file crashes or closes unexpectedly?
Posted: 27 Oct 2020 16:12
by aGerman
Well, you could use a piece of PowerShell code in your batch file. Say, you have a "test.bat" like that:
... which you can close normally by pressing enter, which you could terminate by just closing the window, and which you could also update to make it crash whith a syntax error or whatever.
And you have a "run_and_watch.bat":
Code: Select all
@echo off &setlocal
for /f %%i in (
'powershell.exe -nop -ep Bypass -c "$p=Start-Process -Wait -PassThru -FilePath 'C:\Windows\system32\cmd.exe' -ArgumentList '/c','\"\"test.bat\"\"';$p.ExitCode;"'
) do echo exitcode: %%i
pause
... which runs "test.bat" in a new cmd.exe process, waits for its termination and catches the exit code. Just see what you get depending on how you terminate the test.bat process ...
Steffen
Re: Detect when a batch file crashes or closes unexpectedly?
Posted: 28 Oct 2020 06:26
by Lucky4Me
Thanks to aGerman i made this, maybe it can help
Make a file "test1.bat" with code below
next make a second file "test2.bat" , don't make the file "me.bat" that i use in the code, it's used to crash the batch or trying to simulate a crash
then we have the batchfile "watchdog.bat" that will watch over the above 2 batchfiles
Code: Select all
TITLE Watchdog
CLS
@ECHO OFF
set batch1=test1.bat
set batch2=test2.bat
:startwatch
::CLS
ECHO.
ECHO --------------------Starting watchdog--------------------
ECHO.
set BatchPath="%~dp0"
call:run %batch1%
call:run %batch2%
goto:eof
:run
ECHO.
CLS
ECHO --------------------Starting app--------------------
ECHO.
CD %BatchPath%
for /f %%i in (
'powershell.exe -nop -ep Bypass -c "$p=Start-Process -Wait -PassThru -FilePath 'C:\Windows\system32\cmd.exe' -ArgumentList '/c','\"\"%~1\"\"';$p.ExitCode;"'
) do echo exitcode: %%i & set code=%%i
if %code% == 1 goto problem
if %code% == 0 goto good
:good
ECHO.
ECHO. Everything went ok with %~1.
ECHO. Everything went ok with %~1.>%~1.log
ECHO.
goto:eof
:problem
ECHO.
ECHO. Something went wrong with %~1. Please investigate the cause.
ECHO. Something went wrong with %~1. Please investigate the cause.>%~1.log
ECHO.
goto:eof
:eof
exit
watchdog will create for each batch a log file
Re: Detect when a batch file crashes or closes unexpectedly?
Posted: 28 Oct 2020 12:00
by rasil
Great!
Your program works flawlessly with both of your guys's info I whipped up my own version stated below are all the batch files required to make this work. to test this out please place them in one folder and place your
.exe with them.
1st batch file is called
error.bat
Code: Select all
@echo off
if not exist tmp.tmp exit
del tmp.tmp
echo uh no...
echo.
echo looks like the file you were trying to run exited unexpectedly..
pause
exit
create a .txt file called
filname.txt
Make a
invis.vbs .vbs file
Code: Select all
createobject("wscript.shell").run""""& wscript.arguments(0) &"""",0,false
LAUNCHER.bat
Code: Select all
@echo off
set /p name=<filname.txt
start %name%
if not exist %name% exit
echo .>tmp.tmp
wscript.exe "invis.vbs" "watchdog.bat"
exit
watchdog.bat
Code: Select all
@echo off
if not exist tmp.tmp exit
del tmp.tmp
SETLOCAL EnableExtensions
if not exist filname.txt goto nofil
if exist filname.txt set /p filname=<filname.txt
:loop
set EXE=%filname%
FOR /F %%x IN ('tasklist /NH /FI "IMAGENAME eq %EXE%"') DO IF %%x == %EXE% goto FOUND
goto FIN
:FOUND
echo Running
goto loop
pause
:FIN
:nofil
echo .>tmp.tmp
start error.bat
exit
Make all of these files and put them in
one folder together.
Usage:
1) Place an exe file (to test things out you can make a simple *hello world* batch file and turn that into a exe using a bat2exe converter)
2) open
filname.txt and put your exe's name
with the .exe extension.
3) finally run
LAUNCHER.bat and it should launch your exe file and run silently in the background! try to press the cross or make it exit itself to see an error popup.
But the problem is that this method only works with exe files can anyone help me convert this program to work with batch files?
And just to add it would be amazing if someone integrated all of these batch files to be just 1 batch file!
Thanks for reading and have a good day!

Re: Detect when a batch file crashes or closes unexpectedly?
Posted: 28 Oct 2020 18:25
by Eureka!
I probably missed or misinterpreted some clues here, but shouldn't this be enough? :
Code: Select all
@echo off
setlocal
If /i [%1] == [APE] goto :APE
::__________________________________
::
:: WATCHDOG
::__________________________________
::
TITLE Do not close this!!
Echo this is the watchdog routine. Don't close this.
start /w "RUN APE" "%~f0" APE
Echo APE is no longer running (exited with exit code %ERRORLEVEL%). Do some stuff here
pause
goto :EOF
::__________________________________
::
:APE
::__________________________________
::
Echo this is APE
echo Press a key to exit the APE routine with exit code 5
pause
exit 5
goto :EOF
Re: Detect when a batch file crashes or closes unexpectedly?
Posted: 28 Oct 2020 18:59
by aGerman
Actually there is a difference. If you close the second window via clicking on the red x, it'll trigger a ctrl+c event in the parent process. This is the case if the parent process receives errorlevel -1073741510. (I recall that we have a thread about it somewhere. //EDIT Yeah here you go
viewtopic.php?t=5859#p36423 )
However that's the reason why I used the lengthy workaround by letting the powershell process output the returncode to stdout and capturing it in a FOR /F loop afterwards.
Steffen
Re: Detect when a batch file crashes or closes unexpectedly?
Posted: 30 Oct 2020 08:38
by Eureka!
aGerman wrote: ↑28 Oct 2020 18:59
clicking on the red x,
Right, I missed that one. Thanks for your explanation, Steffen!