Scheduled Task for Windows: Difference between revisions

From Coolscript
Jump to navigation Jump to search
 
(24 intermediate revisions by 2 users not shown)
Line 1: Line 1:
=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. <br>
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.
<br>
<br>
=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. <br>
The script has been tested to run as service using '''srvany.exe''' .
<br><br>


=Features=
=Features=
Line 9: Line 10:
*Protocol events in a xml file
*Protocol events in a xml file
*Log all activities
*Log all activities
<br><br>
<br>


=Requirements=
=Requirements=
*MS Windows 7 or higher or Server2012
*MS Windows 7 or higher or Server2012 or higher
*srvany.exe (to run the script as service)
*srvany.exe (to run the script as service)
*Active State Perl >= 5.14
*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>


Line 32: Line 34:


==Perl Modules==
==Perl Modules==
You need to have a recent version of active state perl installed, when done please run the following installation commands:
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.<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
  ppm install Date-Manip
===Strawberry===
Run '''cpan''' first to get the installation shell.
cpan> '''install Win32:Process:List'''
cpan> '''install Date:Manip'''
<br>
<br>


==Service Registration==
==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:
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
Line 48: Line 61:
<br>
<br>


==Service Configuration==
===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 58: Line 71:
<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 the forward slash as a directory separator like on linux even on windows, this way we get no trouble with escape sequences.<br><br>
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>
Line 76: Line 89:
|  || || StatusFile || Path and Name to Status File, leave empty or set zero to disable
|  || || StatusFile || Path and Name to Status File, leave empty or set zero to disable
|-
|-
|  || || Log || Path to Log Directory
|  || || Log || Switch on logging (0/1)
|-
|-
|  || || LogAge || Path to Log Directory
|  || || LogAge || Maximum logfile age (days) before deleting them.
 
|-
|-
|  || || LogDirectory || Path to Log Directory
|  || || LogDirectory || Path to Log Directory
Line 111: Line 123:




==Examples==
===Examples===
*Run 2 Jobs every 5 and every 20 minutes.<br>
*Run 2 Jobs every 5 and every 20 minutes.<br>
  <?xml version="1.0"?>
  <?xml version="1.0"?>
Line 190: Line 202:
<br><br>
<br><br>


=Status File=
==Status File==
A XML status file is available to obtain the current status of the application,
A XML status file is available to obtain the current status of the application,
the file is read only.<br>
the file is read only.<br>
Line 233: 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