JSI Tip 4663. How can I be notified of failed Scheduled Tasks?

Jerold Schulman

January 8, 2002

2 Min Read
ITPro Today logo in a gray background | ITPro Today


In tip 4532, I described how to be notified if a Scheduled Task is missed.

In tip 2911, I described how to view the results of Scheduled Task processing.

To automatically be notified of failed Scheduled Tasks, I have scripted Schedlgu.bat. The syntax is:

Schedlgu [%ComputerName%]

where %ComputerName% is the computer that will receive the alert of the failed task. If youomit this parameter, the local computer will receive the alert.

NOTE: You can easily modify this script to send the alert to a list of computer and/or user names, or to a group.

NOTE: Schedlgu.bat records the failed events at "%APPDATA%schedlgu.txt", to prevent the sending of duplicate alert messages.

Schedlgu.bat should be scheduled as a background task. When I create the schedule, I set it to run daily and start yesterday and run every 10 minutes from 00:00 to 23:49.

Schedlgu.bat contains:

@echo offsetlocalset notify="%computername%"if not {%1}
{} set notify=%1set job=set done=Nif exist "%temp%schedlgu.tmp" del /q "%temp%schedlgu.tmp"if exist "%temp%schedlgu1.tmp" del /q "%temp%schedlgu1.tmp"if exist "%temp%schedlgu2.tmp" del /q "%temp%schedlgu2.tmp"if exist %temp%schedlgu.txt del /q %temp%schedlgu.txtif not exist "%APPDATA%schedlgu.txt" @echo ** Begin Schedulgu.bat **>"%APPDATA%schedlgu.txt"copy %systemroot%schedlgu.txt %temp%schedlgu.txtfor /f "Tokens=*" %%i in ('type %temp%schedlgu.txt') do set line=%%i&call :parseif not exist "%temp%schedlgu.tmp" goto :EOFfor /f "Tokens=*" %%i in ('type "%APPDATA%schedlgu.txt"') do set line=%%i&call :smallfindstr /L /I /V /G:"%TEMP%schedlgu2.tmp" "%temp%schedlgu.tmp" >>"%TEMP%schedlgu1.tmp"for /f "Tokens=*" %%i in ('type "%TEMP%schedlgu1.tmp"') do set line=%%i&call :senditdel /q "%temp%schedlgu.tmp"del /q "%temp%schedlgu1.tmp"del /q "%temp%schedlgu2.tmp"endlocalgoto :EOF:smallset line=%line:~0,99%@echo %line%>>"%TEMP%schedlgu2.tmp"goto :EOF:senditnet send %notify% "%line%">nul@echo %line%>>"%APPDATA%schedlgu.txt"goto :EOF:parseset line=%line:"=#%set line=%line:(=[%set line=%line:)=]%set line=%line:"=*%if "%line:~0,1%" EQU "*" goto :EOFif "%done%" EQU "Y" goto :EOFif "%line:~0,1%" EQU "#" goto startif "%line%"
"[ ***** Most recent entry is above this line ***** ]" set done=Y&goto startset job=%job%{%line%}goto :EOF:startif "%job%"=="" goto newif "%job:~0,1%" EQU "Y" goto writeset work=%job:{Started =%if NOT "%work%" EQU "%job%" goto newset work=%job:{Result: The task completed with an exit code of [0].}=%if NOT "%work%" EQU "%job%" goto newset work=%job:#Task Scheduler Service#{Exited at=%if NOT "%work%" EQU "%job%" goto new:writeset job=%job:#= %@echo %computername% %job%>>"%temp%schedlgu.tmp":newfor /f "Tokens=1-3* Delims=#*" %%j in ('@echo %line%') do set p3=%%lset job=Nif /i "%p3%" EQU " ERROR " set job=Yif /i "%p3%" EQU " WARNING " set job=Yset job=%job%%line%

Here are some sample alerts:

JSI005 Y At2.job [CMD.EXE /K] 12/31/2001 2:37:00 PM ** ERROR **  {Unable to start task.}{The specific error is:}  {0x80070002: The system cannot find the file specified.}  {Try using the Task page Browse button to locate the application.}JSI005 Y getprod.job [] 12/31/2001 2:53:04 PM ** WARNING **  {Unable to load task.}{The specific error is:}  {0x80070005: Access is denied.}  {Try using the Task page Browse button to locate the application.}JSI005 N At1.job  [CMD]{Finished 12/31/2001 3:14:39 PM}  {Result: The task completed with an exit code of [c000013a].}NOTE: After the computer name, the 1st character is Y for an ERROR or WARNING,        or a N for a non-zero exit code.  



Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like