Scheduled Task for Windows: Difference between revisions

From Coolscript
Jump to navigation Jump to search
(Created page with "=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 ...")
 
No edit summary
Line 44: Line 44:
=Script Configuration=
=Script Configuration=
Description of the configuration file used with st.pl. <br>
Description of the configuration file used with st.pl. <br>
Note: Please use forward slash as a directory separator like on linux even on wondows.
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.<br><br>
 
Default Filename: '''st.xml'''<br><br>
'''Security information:''' Please keep this file secure, only authorized accounts should be able to edit the configuration file.<br><br>
{| class="wikitable " border="1"
{| class="wikitable " border="1"
|+ '''st.xml'''
|+ '''st.xml'''
Line 56: Line 57:
|-
|-
| System || No || ApplicationDirectory || Path to appliactaion  
| System || No || ApplicationDirectory || Path to appliactaion  
|-
|  || || StatusFile || Path and Name to Status File, leave empty or set zero to disable
|-
|  || || Log || Path to Log Directory
|-
|  || || LogAge || Path to Log Directory
|-
|-
|  || || LogDirectory || Path to Log Directory
|  || || LogDirectory || Path to Log Directory
|-
|-
|  || || StatusFile || Path to Status File
 
|-
| ScheduledTask || Yes || Enabled || Job enable (1/0)
| ScheduledTask || Yes || Enabled || Job enable (1/0)
|-
|-
Line 69: Line 76:
|  ||  || ApplicationParameter || Path to the script, eg: c:/scheduled-task/st.xml
|  ||  || ApplicationParameter || Path to the script, eg: c:/scheduled-task/st.xml
|-
|-
|  ||  || ApplicationPriority || Available: '''Below / Normal'''
|  ||  || ApplicationPriority || Available: '''Below''' or  '''Normal'''
|-
|-
|  ||  || Mode || --
|  ||  || Mode || Currently only '''Repeat'''
|-
|-
|  ||  || FrequencyUnit  || --
|  ||  || FrequencyUnit  || M=Every Minute <br> D=Daily <br> W=Weekly
|-
|-
|  ||  || FrequencyInterval || --
|  ||  || FrequencyInterval || Only with Mode=M, the interval in minutes for the scheduled task
|-
|-
|  ||  || FrequencyOffset || --
|  ||  || FrequencyOffset || Only with Mode=M, adding minutes to the next scheduled task
|-
|-
|  ||  || IsUnique || --
|  ||  || 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<br>The following abbrevitions can be used, capitals or ordering do not matter<br>Mo,Tu,We,Th,Fr,Sa,Su<br>Mon,Tue,Wed,Thu,Fri,Sat,Sun<br>Monday,Tuesday,Wednesday,Thuesday,Friday,Saturday,Sunday<br>0,1,2,3,4,5,6<br>
|-
|  ||  || IsUnique || (0/1) Specify if the job is allowed to run once only.
|-
|}




==Examples==
*Run 2 Jobs every 5 and every 2 minutes.<br>
<?xml version="1.0"?>
<CONFIG>
  <!-- Sample config, logging has been switched off !-->
  <System
  ApplicationDirectory="c:/demo/scheduled-task/"
  StatusFile="0"
  Log="0"
  LogAge="0"
  LogDirectory=""
  />
  <!-- Run the following job every 5 minutes, less cpu priority !-->
  <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"
  />
  <!-- Run the following job every 20 minutes, set 5 min. offset !-->
  <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 at 05:00<br>
<?xml version="1.0"?>
<CONFIG>
  <!-- Sample config, logging has been switched on !-->
  <System
  ApplicationDirectory="c:/demo/scheduled-task/"
  StatusFile="0"
  Log="1"
  LogAge="14"
  LogDirectory="c:/demo/scheduled-task/log/"
  />
  <!-- Run the following daily at 01:00, less cpu priority !-->
  <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"
  />
  <!-- Run the following job every working day at 05:00!-->
  <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"
  />


|}
<br><br>
=Status File=
A XML status file is available to obtain the current status of the application,
the file is read only.<br>
Structure:
{| class="wikitable " border="1"
|+ '''st.xml'''
|-
! scope="col" | Element
! scope="col" | Array
! scope="col" | Attribute
! scope="col" | 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)
|-
|  ||  || STOP || The last stop event (yyyy-mm-dd hh:mm:ss)
|-
|  ||  || NEXT || The next start event (yyyy-mm-dd hh:mm:ss)
|-
|}





Revision as of 22:33, 1 January 2014

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.
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

  • srvany.exe
  • Active State Perl >= 5.14
  • Additional Modules:
    • Win32::Process
    • Win32::Process::List


Installation

Unextract 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

Perl Modules

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

ppm install Win32-Process
ppm install Win32-Process-list

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

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 Path to Log Directory
LogAge Path to Log Directory
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 2 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 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)
STOP The last stop event (yyyy-mm-dd hh:mm:ss)
NEXT The next start event (yyyy-mm-dd hh:mm:ss)



Download the script
Download