Monitoring “Last Result” of scheduled tasks on Windows

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[1];
my $monitor = $ARGV[0];
my $resString = qx(schtasks /QUERY /FO LIST /V /TN $jobToCheck | FindStr "Result");
my @resList = split(' ',$resString);
my $lastRun = pop(@resList);
print $lastRun;
Advertisements

Author: bithajcsar

I am a system architect with decades of experience behind. During that time I learned that only the continuous development of knowledge and skills can keep a specialist on the cutting edge of his profession. And only those will move the World forward! Another thing I noticed that the stupidly used ICT (Information and Communication Technology) moved people far from each-other, when the goal of the whole thing should be the opposite. A modern computer may provide too many "instant experience and happiness" and chain users - mainly kids - to their monitors. Just a step away from the Matrix! After all those thoughts I decided to share my knowledge and experience in a form understandable for anyone who wants to know more about devices and systems we use every day. My primary targets are kids. They are curious and they can learn so fast that it sometimes seems unbelievable for us. Maybe they will just know a bit more about ICT and will use that in a more clever way. But who knows? Maybe they will go further and later will move the World forward. I hope that I will be able to help them to find a right way and go much further we could imagine now.

1 thought on “Monitoring “Last Result” of scheduled tasks on Windows”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s