Skip navigation

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


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 you omit 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 off
setlocal
set notify="%computername%"
if not \{%1\}

\{\} set notify=%1 set job= set done=N if 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.txt if not exist "%APPDATA%\schedlgu.txt" @echo ** Begin Schedulgu.bat **>"%APPDATA%\schedlgu.txt" copy %systemroot%\schedlgu.txt %temp%\schedlgu.txt for /f "Tokens=*" %%i in ('type %temp%\schedlgu.txt') do set line=%%i&call :parse if not exist "%temp%\schedlgu.tmp" goto :EOF for /f "Tokens=*" %%i in ('type "%APPDATA%\schedlgu.txt"') do set line=%%i&call :small findstr /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 :sendit del /q "%temp%\schedlgu.tmp" del /q "%temp%\schedlgu1.tmp" del /q "%temp%\schedlgu2.tmp" endlocal goto :EOF :small set line=%line:~0,99% @echo %line%>>"%TEMP%\schedlgu2.tmp" goto :EOF :sendit net send %notify% "%line%">nul @echo %line%>>"%APPDATA%\schedlgu.txt" goto :EOF :parse set line=%line:"=#% set line=%line:(=\[% set line=%line:)=\]% set line=%line:"=*% if "%line:~0,1%" EQU "*" goto :EOF if "%done%" EQU "Y" goto :EOF if "%line:~0,1%" EQU "#" goto start if "%line%"

"\[ ***** Most recent entry is above this line ***** \]" set done=Y&goto start set job=%job%\{%line%\} goto :EOF :start if "%job%"=="" goto new if "%job:~0,1%" EQU "Y" goto write set work=%job:\{Started =% if NOT "%work%" EQU "%job%" goto new set work=%job:\{Result: The task completed with an exit code of \[0\].\}=% if NOT "%work%" EQU "%job%" goto new set work=%job:#Task Scheduler Service#\{Exited at=% if NOT "%work%" EQU "%job%" goto new :write set job=%job:#= % @echo %computername% %job%>>"%temp%\schedlgu.tmp" :new for /f "Tokens=1-3* Delims=#*" %%j in ('@echo %line%') do set p3=%%l set job=N if /i "%p3%" EQU " ERROR " set job=Y if /i "%p3%" EQU " WARNING " set job=Y set 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.  


Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish