Custom scheduled tasks
Application administrators frequently define custom scheduled tasks for their applications running in Windows Server environment. These tasks frequently miss well designed logging features. At the same time monitoring of return values of those tasks can often be important.
If a task misses well-defined logging, gathering information about its results can be difficult and uncertain. In such situation it is essential to monitor the “Last Result” of the scheduled task.
Information sources about task status
Windows stores detailed information about last run off each scheduled task. However, this information is hard to extract in some cases. If the monitoring system used does not have a specialized module for that, one should create that module (script) on his own. Let’s see what kind of information can be accessed by programs in Windows Server.
Log for scheduled tasks
There is a log file (c:\Windows\Tasks\SCHEDLGU.TXT) where the task scheduler writes logging information about tasks it invoked. The size of that log file is fixed: 32kByte. For simplicity, Windows writes the file “in rounds”. In other words, if it reaches the end of the 32kByte, it continues to write records at the beginning of the file overwriting old records. The line:
[ ***** Most recent entry is above this line ***** ]
shows the current, logical end of records in the file. If one want to monitor records in that file, a script should be created for restructuring records in a new file. The new sequential log file can be easily monitored.
Information about last run
Information about last run of a task can be seen on the GUI, but that is not an option for a monitoring module/script. Instead, the “schtasks” command can be used:
C:\Users\xxxxx>schtasks /QUERY /FO LIST /V /TN Analyze_Tables Folder: \ HostName: XXXXXXX TaskName: \Analyze_Tables Next Run Time: 27.04.2012 21:30:00 Status: Ready Logon Mode: Interactive/Background Last Run Time: 26.04.2012 21:30:00 Last Result: 0 Author: XXXXXXXX\xxxxxxxx Task To Run: "E:\Programme\xxxxx\Analyze Tables (Compute Statistics)\Analyze_Tables_Start.cmd Start In: N/A Comment: Task zur Analyse der Tabellen und Indexe in der DB-Instanz b902 Scheduled Task State: Enabled Idle Time: Disabled Power Management: Stop On Battery Mode, No Start On Batteries Run As User: XXXXXXXXX\xxxxxxxxx Delete Task If Not Rescheduled: Enabled Stop Task If Runs X Hours and X Mins: 00:59:00 Schedule: Scheduling data is not available in this format. Schedule Type: Daily Start Time: 21:30:00 Start Date: 20.11.2010 End Date: N/A Days: Every 1 day(s) Months: N/A Repeat: Every: Disabled Repeat: Until: Time: Disabled Repeat: Until: Duration: Disabled Repeat: Stop If Still Running: Disabled
In this output the “Last Result” line is present. To make even more simple to dig out the return value one may use:
C:\Users\xxxxx>schtasks /QUERY /FO LIST /V /TN Analyze_Tables | FindStr "Result" Last Result: 0
Script for finding “Last Result”
In the task I implemented I used the second way to gather information. For extracting the return value from the string above it is better to use some more sophisticated scripting language. For that purpose I decided to use Perl, because Perl interpreter is present in the HP OVO agent used for monitoring at our systems.
Below is the very simple script printing out the return value for the selected task. You may replace the last “print” statement with anything suitable for your task and environment.
# Script checks the "Last Run" parameter of a Windows scheduled task # It prints the value found # # Usage: # name_of_the_script.pl name-of-the-scheduled-task # # use strict; my $jobToCheck = $ARGV; my $monitor = $ARGV; my $resString = qx(schtasks /QUERY /FO LIST /V /TN $jobToCheck | FindStr "Result"); my @resList = split(' ',$resString); my $lastRun = pop(@resList); print $lastRun;