Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Need notification when a batch file stops running

6 views
Skip to first unread message

Dave Pink

unread,
Nov 26, 2001, 10:20:33 AM11/26/01
to
I have a batch file that runs continously, repeating in an endless
loop. I need some simple code to put in this batch file that will
alert me when it stops running. Perhaps some benign task can be
continously performed in a loop, and when that task ceases, that will
be the alert that tells me the batch file has stopped running.

I'll configure the alert to trigger a net send, or maybe trigger
another batch file that will do what I need to do from there. Any
suggestions would be greatly appreciated. Thanks in advance.

William Allen

unread,
Nov 26, 2001, 12:06:26 PM11/26/01
to
"Dave Pink" wrote in message

I can't test in NT, but this is easy in Windows 95/98/ME.

The following Batch file has a demo loop and uses SLEEP
(see note 1). If you don't want to use SLEEP you can use
For the five second delay: choice /c:delay /td,5>NUL
For the ten second delay: choice /c:delay /td,10>NUL
(but see the note for reasons why CHOICE is a bad idea)

Essentially, the Batch immediately launches a Subroutine
in a separate process that checks every 10 seconds to
ensure a marker file has been deleted. If it has it recreates
it, and waits 10 seconds again.

The main part, which contains your infinite loop, starts
each loop by first deleting the marker file if it exists. You
must tune the SLEEP 10 in the subroutine to be always
longer than one loop interval in the main part of the script.

To use the demo, get SLEEP from the URL below, or
replace the lines with the CHOICE equivalent, then
double-click it. The separate _STILLTHERE subroutine
runs as a minimised button on the Taskbar and "watches"
the main process. Within 10 seconds or so of any halt
in the main process (test by killing or forcing window close),
the watcher process ECHOes a message and its button
changes to Finished. You should configure your NET SEND
where I've put this warning message.

====Begin cut-and-paste (omit this line)
@ECHO OFF
IF (GOTO:)==(%1) %1%2 (Subroutine handler)

:: Set warning Subroutine running as a separate process
START /m %0 GOTO: _STILLTHERE

:: This is your code loop - I replace it with a SLEEP 5 statement
:LOOP (infinite loop)
:: Clear the STILLTHERE marker file if it exists
IF EXIST %TEMP%.\STILLTHERE DEL %TEMP%.\STILLTHERE

:: Your loop code goes here
ECHO. We're still here...
SLEEP 5

:: This is the end of your loop
GOTO LOOP

GOTO EOF (=Subroutine code follows=)
:_STILLTHERE
:: Create marker file
ECHO. Still there marker file>%TEMP%.\STILLTHERE
:: Wait 10 seconds for it to be deleted
SLEEP 10
:: Loop back if it's been deleted
IF NOT EXIST %TEMP%.\STILLTHERE GOTO _STILLTHERE
ECHO. Warning - main process has failed to clear marker file!
:: Clear it anyway to be tidy!
DEL %TEMP%.\STILLTHERE

:EOF (End-of-file)

====End cut-and-paste (omit this line)
To use the code above, cut-and-paste the text between the ==== lines
into a file with extension .BAT and base name of your choice. Lines
that don't begin with two spaces have wrapped in transmission. The
Batch code above was written and tested in the Win9x GUI.

--
William Allen

Note 1
SLEEP.EXE: Batch File Wait, causes the computer to wait for a
specified amount of time. Get this utility free from Microsoft:
ftp://ftp.microsoft.com/Services/TechNet/samples/ps/win98/reskit/scrpting/
and click on the SLEEP.EXE Batch Tool.

Utilities Full Documentation - IMPORTANT file
Full documentation, usage instructions, and syntax examples
for Batch usage of all the above Batch tools:
ftp://ftp.microsoft.com/Services/TechNet/samples/ps/win98/reskit/help/win98rk.chm
This is a fully indexed and searchable compiled help file and this
is the VITAL FILE TO DOWNLOAD. Although each utility responds
to the usual /? for brief help, this main file has huge detail. When you
have the file, look in the Contents section under "Scripting Tools".

Note that, while most people know that:
choice /c:delay /td,nn>NUL
can also be used to "sleep" for periods nn = 1 to 99 seconds,
the SLEEP.EXE batch utility differs in that:
1) Sleeps for periods (in seconds) up to many hours, in a single command
2) Doesn't interfere with system performance (the CHOICE delay
method will normally use most of the free CPU time available.
SLEEP.EXE typically consumes under 0.05% of CPU time).

Note 2
For further information about the above utilities, see my post:
From: "William Allen"
Newsgroups: alt.msdos.batch
Subject: Microsoft Batch Tools freely available
Date: Sun, 25 Nov 2001 07:09:43 -0000
Message-ID: <9tq5ha$4981t$1...@ID-55970.news.dfncis.de>


Dave Pink

unread,
Nov 27, 2001, 11:30:03 AM11/27/01
to
"William Allen" <ma...@mayfly13.fsnet.co.uk> wrote:

Thanks William. Your code looks simple and able to do the job. As it
turns out, I won't need to use it (in this case anyway) as I've found
another solution to my problem.

My problem was that I had a batch file that would mysteriously stop
running. The batch file was run as a service using SRVANY.EXE. The
service itself would remain running, yet the batch file would stop at
random times. I was also getting Dr. Watson errors:

"An application error has occurred...
CMD.exe Exception: access violation (0xc0000005) Address: 0x01378b6b"

My solution was to use the freeware FireDaemon (www.firedaemon.com) as
a substitute for SRVANY.EXE. FireDaemon has built in monitoring which
checks the status of the application (batch file) and raises an error
alert in the Application event log if the application should terminate
for any reason. Then it will restart the application automatically.

I was going to use your code to essentially do the same thing.

I've already seen in the Event log where FireDaemon has restarted my
batch file automatically a couple times overnight, so my problem is
solved. I'm still getting occasional Dr. Watson errors, but I can live
with that.

Thanks again William.

Dave

0 new messages