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 ...")
 
 
(33 intermediate revisions by 2 users not shown)
Line 1: Line 1:
=About=
=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. <br>
This is an 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. <br>
The script has been tested to run as service using '''srvany.exe''' .
The script has been tested to run as service using '''srvany.exe''' but it is possible to run the script in a common way
<br><br>
(eg: perl st.pl -c path/configfile) but we intend to run it as service.
 
<br>
 
=Features=
=Features=
*Can run scheduled programs every minute, hour, day or week
*Can run scheduled programs every minute, hour, day or week
*Protocol events in a xml file
*Protocol events in a xml file
*Log all activities
*Log all activities
<br><br>
<br>


=Requirements=
=Requirements=
*srvany.exe
*MS Windows 7 or higher or Server2012 or higher
*Active State Perl >= 5.14
*srvany.exe (to run the script as service)
*Active State Perl >= 5.14 or Strawberry Perl >=5.22
*Additional Modules:
*Additional Modules:
**Win32::Process
**Win32::Process
**Win32::Process::List
**Win32::Process::List
**Date::Manip
<br>
<br>
=Installation=
=Installation=
Unextract and copy the files to a directory of you your choice, this example is using c:\scheduled-task
Uncompress and copy the files to a directory of you your choice, this example is using c:\scheduled-task
<br>
<br>
These are the required script files:
These are the required script files:
*c:\scheduled-task\st.pl
*c:\scheduled-task\st.pl
*c:\scheduled-task\st.xml
*c:\scheduled-task\st.xml
There is a optional plugin for Nagios / Icinga available, please see:
*check_st.pl
<br>


==Perl Modules==
==Perl Modules==
You need to have a recent version of active state perl installed, please run
It is possible to use either Active State or Strawberry Perl
the installation commands:
===Active State===
You need to have a recent version of active state perl installed, when done please run the following installation commands.<br>
Note that Active State doesn't allow Date-Manip to be installed with it's recent community version as of December 2015.
  ppm install Win32-Process
  ppm install Win32-Process
  ppm install Win32-Process-list
  ppm install Win32-Process-list
ppm install Date-Manip
===Strawberry===
Run '''cpan''' first to get the installation shell.
cpan> '''install Win32:Process:List'''
cpan> '''install Date:Manip'''
<br>
==Service==
Note: It is possible to install the service with or without a service description.


==Service Registration==
===Service Registration===
Get the tool srvany.exe and copy it to c:\windows\system32\, then register the service by running:
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
  sc create Perl-Scheduled-Task binPath= C:\Windows\System32\srvany.exe DisplayName= Perl-Scheduled-Task


==Service Configuration==
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.
<br>
 
===Service Configuration===
Open your registry and add the key '''Parmeters''':
Open your registry and add the key '''Parmeters''':
  [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Perl-Scheduled-Task\'''Parameters''']
  [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Perl-Scheduled-Task\'''Parameters''']
Line 42: Line 70:


<br><br>
<br><br>
=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 86:
|-
|-
| 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 || Switch on logging (0/1)
|-
|  || || LogAge || Maximum logfile age (days) before deleting them.
|-
|-
|  || || 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 104:
|  ||  || 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 20 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 (mo-fr) 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)
|-
|  ||  || END || The last end event (yyyy-mm-dd hh:mm:ss)
|-
|  ||  || NEXT || The next start event (yyyy-mm-dd hh:mm:ss)
|-
|}




Line 97: Line 245:
|style="width:10%; vertical-align: top;"|
|style="width:10%; vertical-align: top;"|
'''Download the script'''<br>
'''Download the script'''<br>
[[File:Download.png|60px|link=http://coolscript.org/download/st.zip|Download]]
[[File:Download.png|60px|link=https://coolscript.org/download/scripts/st.zip|Download]]
<br>
<br>
|style="width:90%; vertical-align: top;"|
|style="width:90%; vertical-align: top;"|

Latest revision as of 13:09, 4 March 2022

About

This is an 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 but it is possible to run the script in a common way (eg: perl st.pl -c path/configfile) but we intend to run it as service.


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 or higher
  • srvany.exe (to run the script as service)
  • Active State Perl >= 5.14 or Strawberry Perl >=5.22
  • Additional Modules:
    • Win32::Process
    • Win32::Process::List
    • Date::Manip


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

It is possible to use either Active State or Strawberry Perl

Active State

You need to have a recent version of active state perl installed, when done please run the following installation commands.
Note that Active State doesn't allow Date-Manip to be installed with it's recent community version as of December 2015.

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

Strawberry

Run cpan first to get the installation shell.

cpan> install Win32:Process:List
cpan> install Date:Manip


Service

Note: It is possible to install the service with or without a service description.

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 (days) 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