Sys::Manage::Schedule - Scriptic schedule trunk
use Sys::Manage::Schedule; my $s =Sys::Manage::Schedule->new([@ARGV], -option=>value,..); if ($s->at('-a')) { # at any other item execution ... } if ($s->at('-' # at system scheduler arguments given ,'17:20','/interactive','/every:M,T,W,Th,F,S,Su') { ... } if ($s->at('-' # ... automatically filling arguments repeating ,['17:23','/interactive','/every:M,T,W,Th,F,S,Su'] ,['17:24'])) { ... } if ($s->at('-s',0)) { # at operating system startup, if automatic MSWin32 service ... } if ($s->at('-d0')) { # at the beginning of the day ... } if ($s->at('-d2s')) { # at the end of the day, switching user ... } if($s->at('-' # at perl atSched.pl -run test ,'test')) { ... } if($s->at('-s' # at perl atSched.pl -surun test ,'test')) { ... } if ($s->at('-s' ,'smrmt')) { # at perl atSched.pl -surun-l smrmt command-line $s->run('smrmt.bat', $s->qclad(@ARGV[2..$#ARGV])) }
perl atSched.pl -runmod atarg -set (default) add/replace scriptic schedule into system schedule -setdel clear scriptic schedule from system schedule -run atarg run 'atarg' entry from system scheduler -surun atarg run 'atarg' entry switching user -surun-l ... displaying output even from MSWin32 service -surun-seconds... calling soon('-', seconds, 'self', '-run',...) -svcinst install scriptic schedule as MSWin32 service to switch user -svcinst user password -svcdel remove scriptic schedule MSWin32 sevice -logask filter and display log file -run name arg... running as named command line, -surun name arg... user may be switched
-a run at any '-run'/'-runsu' -s run switching user -s => 0 ... at automatic MSWin32 service startup -w redirect stdout and stderr into log file -d0 -d1 -d2 at day begin, middle, end -n0 -n1 -n2 at night begin, middle, end
The purpose of this module is to implement a scriptic schedules or scripts for scheduling and executing Perl codes inside.
A benefit of this module is preliminary for MSWin32 platform with 'at' commands and impersonations, then for UNIX where 'crontab':
* Keeping persistent or significant scheduled commands in a file (at, -set).
at
-set
* Automatically filling repeating scheduler arguments (at).
* Running impersonated (switched user, -s).
-s
* Logging of execution (-dirv, -w, runlog, vfname).
-dirv
-w
runlog
vfname
* Scheduling relative at day or night begin/middle/end (-d0,-d1,-d2,-n0,-n1,-n2).
-d0
-d1
-d2
-n0
-n1
-n2
* Scheduling cyclic tasks (soon)
soon
* Scheduling at operating system startup (-s,0).
This module is implemented and tested with Perl 5.6/5.8 on MSWin32 2000/2003.
Usually no options need to be mentioned in a scriptic schedule.
=>8
=>12
=19
Day begin, middle, end hour. See also AT OPTIONS, -n0, -n1, -n2.
AT OPTIONS
=> base directory
Base directory of script execution, filled automatically using $0.
=> directory marker sign
Directory marker, '/' or '\', filled automatically using $0 or $^O.
=> variables directory
Directory for log and flag files. Default is existing '-dirb/var' subdirectory, or -prgcn will be created. Log file automatic truncation implemented, see -logmax.
-dirb
-prgcn
-logmax
=> about 1024*1024 | false
Maximum size of log file, bytes. Used for automatic truncation.
=>22
=>0
=5
Night begin, middle, end hour. See also AT OPTIONS, -d0, -d1, -d2.
=> f($0)
Common name of script, used also in -dirv subdirectory, filled automatically using $0.
Full file name of script, filled automatically using $0.
Short file name of script, used also as MSWin32 service name (-svcinst), filled automatically using $0.
-svcinst
=> run mode
Run mode of script. To be obtained from command line.
See also RUN MODES.
RUN MODES
=> schedule arguments escaped
Schedule entry to be executed while some -runmod (-run, -runsu). To be obtained from command line.
-runmod
-run
-runsu
=> time()
=> localtime()
Time and localtime when script started
The main methods are new and at.
new
Header of scriptic schedule entry. Or scriptic schedule entry itself, if sub{} given. Allows execution of entry if -run or -runsu. Or sets corresponding system scheduler entries if -set.
Special entryName '0' is intended for operating system startup event (may be implemented, i.e., as a result of -svcinst).
See also AT OPTIONS, soon.
Changes MSWin32 user and service password. Service name estimated is -prgsn. Default user is -prgcn. Default password is autogenerated. Additional computers and/or services will be processed after local, treating empty computer as local and empty service as -prgsn.
-prgsn
Implemented for MSWin32 only, Windows Resource Kit used.
This is an utility methods from Sys::Manage::CmdFile may be useful sometimes.
cmdfile proceeds text file interpreting rows as commands for script or sub{} and moving rows proceeded to history file if given.
cmdfile
cmdfck checks commands file for command rows present. If commands found, executes script or sub{} given once, for file names.
cmdfck
Examples with Sys::Manage::Cmd script:
$s->runlog('perl', './bin/smcmv.pl', '-vct', '-gx', 'cmdfile' , './var-cmd/cmd-night-do.txt', 'cmd-night-done.txt','cmd-log.txt'); $s->cmdfile(sub{$s->runlcl("perl ./bin/smcmv.pl -vt $_") ||die $?>>8} , './var-cmd/cmd-night-do.txt', 'cmd-night-done.txt','cmd-log.txt'); $s->cmdfck(sub{$s->runlcl("perl ./bin/smcmv.pl -vct -gx cmdfile $_")} , './var-cmd/cmd-night-do.txt', 'cmd-night-done.txt','cmd-log.txt'); $s->cmdfck(sub{$s->runlog("perl ./bin/smcmv.pl -vct -gx cmdfile", @_)} , './var-cmd/cmd-night-do.txt', 'cmd-night-done.txt','cmd-log.txt'); $s->runlog('perl', './bin/smcmv.pl', '-vct', '-gx', 'cmdfile' , './var-cmd/cmd-night-assign.txt', '""', 'cmd-log.txt'); $s->runlog('perl', './bin/smcmv.pl', '-vct', '-gx', 'cmdfile' , './var-cmd/cmd-night-sched.txt', '""', 'cmd-log.txt');
Suppresses STDIN, STDOUT, STDERR and calls setsid. See perlipc.
Truncates file to -logmax, if greater then.
Reads file and returns content as a string.
Writes file or appends to it.
DNS domain, host, node name of this computer.
Filter log file.
Options: 'v'erbose output (default), 's'tring return (for function call); '>=' or '<=' start and end conditions (default), '>' or '<' conditions.
Log file: file path and name, w32:eventLogName, default is log of this script.
Date format: any of /([ymdhs]+\W)+/, default is 'yyyy-mm-dd hh:mm:ss'
Start and End: 'yyyy-dd-mm...' row sample; /-[\d]+[dhm]/ - days, hours, minutes ago.
Query: 'all' (default), 'pid's running, 'err'ors, 'warn'ings and errors, expression.
Iterator marker switch may be within 'Options' or ending 'Start': '-mm' - move next, '-ma' - move next for 'All' only, '-mu' - use current.
Creates new Sys::Manage::Schedule object. See also set syntax.
set
Prints STDOUT with $|=1. May be useful where output redirections.
Executes operating system command alike system(). Using runlcl, only command line and exit code will be logged. For runlog redirection, IPC::Open3 used, instead of immediate STDOUT/STDERR redirection in -w.
runlcl
Retrieves and sets values of the SLOTS. $s->{-option} direct access may be used also, but set smartly processes some options.
SLOTS
Cyclic entry in scriptic schedule. Sub{} will be executed after seconds given since its previous execution. The first sub{} execution depends on the script executions.
Implemented for MSWin32 only.
See also at, -i, -s, -w.
-i
Schedules command line given with delay, -i option is available. Alike 'soon' utility in Windows Resource Kit.
Initialises execution of the schedule script. Called by the first at if not yet.
File name, based on -dirv, -dirm, -prgcn. Log file name is vfname('log.txt'); lock file name is vfname('lck.txt'); Additional lock file name is vfname('lcs.txt'); -d0 flag file name is vfname('d0.txt'). This method may be useful forming names of additional log files.
-dirm
at call options considered as a first argument beginning with '-' sign. Several options may be catenated into options string without additional '-' signs.
Execute at any -run ('-a') or -runsu ('-as').
Execute daily at the beginning/middle/end of the day, or nightly at the beginning/middle/end of the night. Only one of this options may be included in each at.
Execute interactivelly, Only for soon and -d0 etc.
Execute switching user (-runsu).
On MSWin32 user may be switched using impersonated service (installed via -svcinst), or via WMI (-susr and -spsw required).
-susr
-spsw
On other platforms 'su' is called by 'root', 'sudo' otherwise.
Special at('-s',0) form means automatic MSWin32 service startup at operating system startup.
Write (redirect) STDOUT/STDERR into log file. See better runlog, instead of -w, which may not work sometimes.
Usually operator need not deal with any run mode, because -set is default, when empty arguments.
The mainly used run modes are -set, -setdel, -run, -surun/-runsu.
-setdel
-surun
Supplemental run modes are -logask, -svcinst, -svcdel.
-logask
-svcdel
Additional '-e' option of the run mode (-run-e, -runsu-e, -surun-e, -surun-seconds-e, -surun-l-e) is to consider additional to -runarg command-line arguments (@ARGV[2..$#ARGV]) as escaped and unescape it.
-surun-seconds
-surun-l
-runarg
Filter and display log file, see logask.
logask
Run -runarg entry.
Run -runarg entry with at(-s) option after user is switched.
-surun schedule script execution starts -runsu schedule script execution using -susr/-spsw or -prgsn MSWin32 service installed with -svcinst.
Remove existing scriptic schedule entries (recognized using -prgfn) from the system schedule ('at' or 'crontab'). Add actual scriptic schedule using at method calls resulting in 'at' or 'crontab' calls.
-prgfn
Remove existing scriptic schedule entries (recognized using -prgfn) from the system schedule ('at' or 'crontab').
Remove -prgsn MSWin32 service.
See also -svcinst.
Create -prgsn MSWin32 service using 'instsrv' and 'srvany' from Windows Resource Kit. Additional command line arguments are user name and password.
Service may be needed to switch user (-s, -runsu).
Service may be deleted using -svcdel.
Settings of service created should be checked.
Switch user and execute -runsu -runarg. Additional command line arguments specified after -runarg will be transferred to -runsu.
See also -s.
Additional '-l' option of -surun, is to display output of -runsu. This is useful on MSWin32 where -surun does not do this, see -s.
Adding digits to -surun execute soon('-', seconds, 'self', '-run',...) and display it's output.
Implemented and tested with Perl 5.6.0 and 5.8.8 on Windows 2000/2003.
Some functioning is for MSWin32 only.
See at the top of the source code.
New -surun-l, -surun-seconds, -runmod-e. Output of any named command in the schedule script may be achieved simply.
-runmod-e
Corrected some error() calls.
error
File locks rearranged. Serialised startup of -surun and -runsu, execution of soon and chpswd.
chpswd
logask can now convert digital date-times reading log file.
runlog can now distinguish STDERR and log this as errors.
Introduced iterator marker switch for logask.
Several corrections in logask, run, runopen.
run
runopen
Named command lines implemented: perl atSched.pl -run|-surun name arg... -surun additional command line arguments specified after -runarg will be transferred to -runsu.
soon extended to accept program arguments as list.
So, schedule script may be used also to log some unforeseen commands.
Changed format of log file and messages.
-logpid removed - use logask command instead.
-logpid
New fread, hostdomain, hostname, hostnode methods.
fread
hostdomain
hostname
hostnode
Behaviour of -logpid files improved, content changed, cleanup implemented.
$ENV{SMSECS} may be used for cmdfile and cmdfck.
New -logpid to create runtime pid file for monitoring and diagnostics during runtime.
New cmdfck method.
Published 0.50 version.
Started
This is free software; you can use redistribute it and/or modify it under the same terms as Perl itself.
Andrew V Makarow <makarow at mail.com>, for tav
To install Sys::Manage::Cmd, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Sys::Manage::Cmd
CPAN shell
perl -MCPAN -e shell install Sys::Manage::Cmd
For more information on module installation, please visit the detailed CPAN module installation guide.