Scheduled Task for Windows

From Coolscript
Jump to navigation Jump to search


About

This is a alternative and easy way to run scheduled tasks on Windows. It is useful to workaround access or group policy restrictions, for example if your group policy logs you out after a short period and you want your scheduled tasks keep on running.
The script has been tested to run as service using srvany.exe .

Features

  • Can run scheduled programs every minute, hour, day or week
  • Protocol events in a xml file
  • Log all activities


Requirements

  • MS Windows 7 or higher or Server2012
  • srvany.exe (to run the script as service)
  • Active State Perl >= 5.14
  • Additional Modules:
    • Win32::Process
    • Win32::Process::List


Installation

Uncompress and copy the files to a directory of you your choice, this example is using c:\scheduled-task
These are the required script files:

  • c:\scheduled-task\st.pl
  • c:\scheduled-task\st.xml

There is a optional plugin for Nagios / Icinga available, please see:

  • check_st.pl


Perl Modules

You need to have a recent version of active state perl installed, when done please run the following installation commands:

ppm install Win32-Process
ppm install Win32-Process-list
ppm install Date-Manip


Service

The service description is optional, you can run the script in a common way (eg: perl st.pl -c path/configfile) but we intend to run it as service.

Service Registration

Get the tool srvany.exe and copy it to c:\windows\system32\, then register the service by running:

sc create Perl-Scheduled-Task binPath= C:\Windows\System32\srvany.exe DisplayName= Perl-Scheduled-Task

Optional you may want to set a description for the service:

sc description Perl-Scheduled-Task "Schedule Task Manager for Perl"

Please don't forget to set enough permissions for the new task.

Service Configuration

Open your registry and add the key Parmeters:

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Perl-Scheduled-Task\Parameters]
  • Add a string value called Application within Parameters and add the path to perl and the path to the script:
"Application"="c:\\perl\\bin\\perl.exe c:\\scheduled-task\\st.pl -c c:\\scheduled-task\\st.xml"
  • Check your service permission, make sure the service user has enough permissions to start applications and has edit and create right in its application directory.



Script Configuration

Description of the configuration file used with st.pl.
Note: Please use the forward slash as a directory separator like on linux even on windows, this way we get no trouble with escape sequences.

Default Filename: st.xml

Security information: Please keep this file secure, only authorized accounts should be able to edit the configuration file.

st.xml
Element Array Attribute Description
System No ApplicationDirectory Path to appliactaion
StatusFile Path and Name to Status File, leave empty or set zero to disable
Log Switch on logging (0/1)
LogAge Maximum logfile age before deleting them.
LogDirectory Path to Log Directory
ScheduledTask Yes Enabled Job enable (1/0)
Name Any Jobname
ApplicationPath Path to perl, eg: c:/perl64/bin/perl.exe
ApplicationParameter Path to the script, eg: c:/scheduled-task/st.xml
ApplicationPriority Available: Below or Normal
Mode Currently only Repeat
FrequencyUnit M=Every Minute
D=Daily
W=Weekly
FrequencyInterval Only with Mode=M, the interval in minutes for the scheduled task
FrequencyOffset Only with Mode=M, adding minutes to the next scheduled task
FrequencyTime Only with Mode=D or W, the time (HH:MM) for the scheduled task
FrequencyDay Only with Mode=W, a csv list of the weekday for the scheduled task
The following abbrevitions can be used, capitals or ordering do not matter
Mo,Tu,We,Th,Fr,Sa,Su
Mon,Tue,Wed,Thu,Fri,Sat,Sun
Monday,Tuesday,Wednesday,Thuesday,Friday,Saturday,Sunday
0,1,2,3,4,5,6
IsUnique (0/1) Specify if the job is allowed to run once only.


Examples

  • Run 2 Jobs every 5 and every 20 minutes.
<?xml version="1.0"?>
<CONFIG>
 <System
  ApplicationDirectory="c:/demo/scheduled-task/"
  StatusFile="0"
  Log="0"
  LogAge="0"
  LogDirectory=""
 />
 <ScheduledTask
  Enabled="1"
  Name="Demo Job"  
  ApplicationPath="c:/perl/bin/perl.exe"
  ApplicationParameter="C:/demo/job.pl"
  ApplicationPriority="Below"
  Mode="Repeat"
  FrequencyUnit="M"  
  FrequencyInterval="5"  
  FrequencyOffset="0"  
  IsUnique="1"
 />
 <ScheduledTask
  Enabled="1"
  Name="Demo Job"  
  ApplicationPath="c:/perl/bin/perl.exe"
  ApplicationParameter="C:/demo/anotherjob.pl"
  ApplicationPriority="Normal"
  Mode="Repeat"
  FrequencyUnit="M"  
  FrequencyInterval="20"  
  FrequencyOffset="5"  
  IsUnique="1"
 />


  • Run 2 Jobs every day at 01:00 and every working day (mo-fr) at 05:00
<?xml version="1.0"?>
<CONFIG>
 <System
  ApplicationDirectory="c:/demo/scheduled-task/"
  StatusFile="0"
  Log="1"
  LogAge="14"
  LogDirectory="c:/demo/scheduled-task/log/"
 />
 <ScheduledTask
  Enabled="1"
  Name="Demo Job"  
  ApplicationPath="c:/perl/bin/perl.exe"
  ApplicationParameter="C:/demo/job.pl"
  ApplicationPriority="Below"
  Mode="Repeat"
  FrequencyUnit="D"  
  FrequencyTime="01:00"  
  IsUnique="1"
 />
 <ScheduledTask
  Enabled="1"
  Name="Demo Job"  
  ApplicationPath="c:/perl/bin/perl.exe"
  ApplicationParameter="C:/demo/anotherjob.pl"
  ApplicationPriority="Normal"
  Mode="Repeat"
  FrequencyUnit="W"  
  FrequencyDay="mo,tu,we,th,fr"  
  FrequencyTime="05:00"  
  IsUnique="1"
 />



Status File

A XML status file is available to obtain the current status of the application, the file is read only.
Structure:

st.xml
Element Array Attribute Description
Status No Process Scheduled Task Process Information
Pid The Process Identifier
StartDate Startup Date (yyyy-mm-dd hh:mm:ss)
LastCheckDate Last Event Date (yyyy-mm-dd hh:mm:ss)
ScheduledTask Yes Name The Job Name
Enabled (0/1) Job Enabled
PID The current PID of the scheduled task, is zero if the process does not run
START The last start event (yyyy-mm-dd hh:mm:ss)
END The last end event (yyyy-mm-dd hh:mm:ss)
NEXT The next start event (yyyy-mm-dd hh:mm:ss)



Download the script
Download