Detect when a batch file crashes or closes unexpectedly?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
rasil
Posts: 31
Joined: 23 Apr 2020 13:05

Detect when a batch file crashes or closes unexpectedly?

#1 Post by rasil » 27 Oct 2020 05:24

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! :D

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Detect when a batch file crashes or closes unexpectedly?

#2 Post by aGerman » 27 Oct 2020 16:12

Well, you could use a piece of PowerShell code in your batch file. Say, you have a "test.bat" like that:

Code: Select all

@echo off
pause
... 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

Lucky4Me
Posts: 24
Joined: 21 Oct 2020 06:33

Re: Detect when a batch file crashes or closes unexpectedly?

#3 Post by Lucky4Me » 28 Oct 2020 06:26

Thanks to aGerman i made this, maybe it can help

Make a file "test1.bat" with code below

Code: Select all

@echo off
pause
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

Code: Select all

@echo off
pause
start /k me.bat
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

rasil
Posts: 31
Joined: 23 Apr 2020 13:05

Re: Detect when a batch file crashes or closes unexpectedly?

#4 Post by rasil » 28 Oct 2020 12:00

Great! :D

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! :)

Eureka!
Posts: 137
Joined: 25 Jul 2019 18:25

Re: Detect when a batch file crashes or closes unexpectedly?

#5 Post by Eureka! » 28 Oct 2020 18:25

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

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Detect when a batch file crashes or closes unexpectedly?

#6 Post by aGerman » 28 Oct 2020 18:59

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

Eureka!
Posts: 137
Joined: 25 Jul 2019 18:25

Re: Detect when a batch file crashes or closes unexpectedly?

#7 Post by Eureka! » 30 Oct 2020 08:38

aGerman wrote:
28 Oct 2020 18:59
clicking on the red x,
Right, I missed that one. Thanks for your explanation, Steffen!

Post Reply