#!/bin/ksh PATH=$(PATH=/usr/bin:/bin getconf PATH) PATH=/usr/local/bin:$PATH FPATH=/usr/local/fbin z=; unset z [[ $0 == /* ]] && z=$0 || z=$(pwd)/$0 [[ $1 = "help=y" ]] && { shdoc file=$0; exit 0; } if [[ $1 == "--use-logez" ]] then shift else logez device=file keep=30 silent=no checkerror=yes -- $z "$@"; exit $? fi cur_dir=; unset cur_dir prog_name=; unset prog_name log_dir=; unset log_dir lock=; unset lock cur_dir=${z%/*} prog_name=${z##*/} prog_name=${prog_name%.ksh} log_dir=${cur_dir}/log/${prog_name} lock=${log_dir}/lock lock_unlock action=lock name=${lock} expire=86400 wait=3600 id=${prog_name} php_prog=; unset php_prog perl_prog=; unset perl_prog python_prog=; unset python_prog sh_prog=; unset sh_prog x_prog=; unset x_prog php_prog=$cur_dir/${prog_name}.php perl_prog=$cur_dir/${prog_name}.pl python_prog=$cur_dir/${prog_name}.py sh_prog=$cur_dir/${prog_name}.sh c_prog=$cur_dir/${prog_name} for i in $php_prog $perl_prog $python_prog $sh_prog $c_prog do [[ -x $i ]] && { x_prog=$i break } done status=; unset status if [[ -n "$x_prog" ]]; then timeout -s TERM 1800 $x_prog status=$? else printf "%s\n" "ERROR: I have nothing to run." status=1 fi uname=; unset uname mail=; unset mail uname=$(uname -n) mail=$(whence mailx || whence mail) email="root" if (( status == 0 )) then date_no=; unset date_no date_no=$(date +%w) (( date_no == 1 )) && { ${mail} -s "$z succeeded on ${uname}." $email < /dev/null } else ${mail} -s "$z failed on ${uname}." $email < /dev/null fi lock_unlock action=unlock name=${lock} printf "%s\n" "status=${status}" exit ${status} ## POD_START ## =head1 NAME ## ## B - ksh wrapper program to run jobs usually written by other ## languages, providing logistic services. ## ## =head1 SYNOPSIS ## ## vi foo.pl ## ## cp jobsys.ksh foo.ksh ## ## ./foo.ksh ## ## =head1 DESCRIPTION ## ## =head2 WHAT DOES JOBSYS DO? ## ## B provide the following 4 logistic services: ## ## =over 4 ## ## =item (1) One program runing at a time ## ## When a program is scheduled to run at crontab, the program will run ## at given time regardless if the previous run has finished. This could ## lead to unpredictiable results. It could be get things corrupted. ## ## B ensures the only one instance of program running at same ## time. The other program will either wait for the lock to be released, ## or times out. ## ## This service is provided by lock_unlock shell function. For the details ## of the function, please read publication 006 from URL: ## ## http://www.unixlabplus.com/unix-prog/Publication.txt ## ## =item (2) Termination over maximum run time ## ## B will send a kill signal to your job when it exceeds ## the maximum run time. ## ## This service is provided by either Linux /usr/bin/timeout from ## netatalk-2.0.2-3 package, or by timeout.c that I wrote. For details ## of timeout, please read the man page, or the following URL: ## ## http://www.unixlabplus.com/unix-prog/timeout/ ## ## =item (3) Log maintenance ## ## B will create and maintain the log for you. It will ## create the log file as: ## ## /log//.log. ## ## where is presented in ISO 8601 date format [3], for ## example: 2006-01-27T15:26:31. "keep=I" option removes logs beyond ## I days. ## ## This service is provided by I shell functions. ## ## This service will eliminate the hardcoding and management of the logs ## entered in crontab, for example. ## ## 00 03 * * * /path/to/foo >> /path/to/foo.log ## ## =item (4) Notification ## ## B will send an email if your job fails. In order for this ## to work properly, your job has to have proper exit code. Exit code 0 ## means success, others mean failure. ## ## =head2 WHAT IS REQUIRED TO USE JOBSYS ## ## /usr/local/bin/jobsys.ksh ## /usr/local/fbin/lock_unlock ## /usr/local/fbin/logez ## /usr/bin/timeout || /usr/local/bin/timeout ## /usr/local/fbin/shdoc ## ## All of these programs are available from URL: ## ## http://www.unixlabplus.com/unix-prog/ ## ## =head2 HOW TO USE JOBSYS ## ## =item (1) Create your own job, say foo.pl, under . ## ## =item (2) Copy jobsys.ksh to foo.ksh, under . ## ## cp /usr/local/bin/jobsys.ksh /foo.ksh ## ## =item (3) Modify /foo.ksh if necessary ## ## =item (4) Run /foo.ksh ## ## That is it. ## ## =head2 TODO ## ## =item (1) notification (versus termination) over maximum run time ## ## =item (2) notification at certain time even when job succeed. ## ## =head1 AUTHOR ## ## Michael Wang >. ## ## =head1 VERSION HISTORY ## ## Version 1.1, 2006-03-08, Michael Wang, . ## ## * Change kshdoc to shdoc. ## ## Version 1.0, 2006-03-08, Michael Wang, . ## ## * IPO. ## POD_STOP