#!/usr/bin/ksh93 ################################################################ function usagemsg_mkAtJob { print " Program: mkAtJob Description: Schedule a command to run sometime in the future via an \"at\" job, by adding a specified number of days, hours, and/or minutes to the current date/time. Usage: ${1##*/} [-?vV] [-N] | [-t YYYYMMDDHHMM] | [ [-n #Secs|-s YYYYMMDDHHMM] [-D DAYS] [-H HOURS] [-M MINUTES] ] [-j] -c \"Command\" Where: -v = Verbose mode -V = Very Verbose Mode -D DAYS = Number of days from NOW to run the \"at\" job (Default:1) -H HOURS = Number of hours from NOW to run the \"at\" job (Default:0) -M MINUTES = Number of minutes from NOW to run the \"at\" job (Default:0) -n NOW = Number of Seconds since the epoch to use as the \"NOW\" date/time. (Default:NOW) -s YYYYMMDDHHMM = Date and time to use as the \"NOW\" date/time. (Default:None) -t YYYYMMDDHHMM = Run the \"at\" job at the specified date and time. (Default:None) -N = Run the \"at\" job using the keyword \"now\" as the time to run the Job. -j = Print the \"at\" job number on STDOUT after scheduling command -c Command = A unix command to schedule to run thru \"at\" job scheduler. Example Usage: mkAtJob -D 2 -H 6 -M 30 -c \"cat /etc/passwd > /tmp/passwd.out\" mkAtJob -s 201309091103 -D 2 -c \"cat /etc/passwd > /tmp/passwd.out\" mkAtJob -t 201309091103 -c \"cat /etc/passwd > /tmp/passwd.out\" Author: Dana French (dfrench@mtxia.com) Copyright 2013 \"AutoContent\" enabled " } ################################################################ #### Program: mkAtJob #### #### Description: #### #### Author: Dana French (dfrench@mtxia.com) #### #### Date: 09/11/2013 #### ################################################################ function mkAtJob { typeset TRUE="0" typeset FALSE="1" typeset VERBOSE="${FALSE}" typeset VERYVERB="${FALSE}" typeset MKAT_CMD="" typeset MKAT_DAY="0" typeset MKAT_HRS="0" typeset MKAT_MIN="0" typeset MKAT_ADD="0" typeset MKAT_SEC="0" typeset MKAT_NOW=$( printf "%(%s)T\n" ) typeset MKAT_TIM="${MKAT_NOW}" typeset MKAT_DTM="${MKAT_NOW}" typeset MKAT_NUM="${FALSE}" typeset MKAT_KEY="${FALSE}" typeset MKAT_TKY="${FALSE}" while getopts ":vVD#H#M#c:n:s:t:jN" OPTION do case "${OPTION}" in 'D') MKAT_DAY="${OPTARG}";; 'H') MKAT_HRS="${OPTARG}";; 'M') MKAT_MIN="${OPTARG}";; 'n') MKAT_NOW="${OPTARG}";; 's') MKAT_NOW=$( printf "%(%s)T\n" ${OPTARG} );; 't') MKAT_NOW=$( printf "%(%s)T\n" ${OPTARG} ) MKAT_TKY="${TRUE}";; 'c') MKAT_CMD="${OPTARG}";; 'j') MKAT_NUM="${TRUE}";; 'N') MKAT_KEY="${TRUE}";; 'v') VERBOSE="${TRUE}";; 'V') VERYVERB="${TRUE}";; '?') usagemsg_mkAtJob && return 1;; esac done shift $(( ${OPTIND} - 1 )) ################################################################ trap "usagemsg_mkAtJob ${0}" EXIT if (( MKAT_NOW < MKAT_TIM )) then print -u 2 -- "# ERROR: The date/time specified is in the past." print -u 2 -- "# ERROR: Please specify a date/time in the future." return 2 fi if [[ "_${MKAT_CMD}" == "_" ]] then print -u 2 -- "# ERROR: Command to schedule as \"at\" job not specified." return 3 fi trap "-" EXIT ################################################################ (( VERYVERB == TRUE )) && VERBOSE="${TRUE}" (( VERBOSE == TRUE )) && print -u 2 -- "###################################" (( VERYVERB == TRUE )) && set -x ################################################################ if (( MKAT_KEY == TRUE )) then MKAT_TIM="now" elif (( MKAT_TKY == TRUE )) then MKAT_TIM=$( printf "%(%Y%m%d%H%M)T\n" "#${MKAT_NOW}" ) MKAT_TIM="-t ${MKAT_TIM}" else (( VERBOSE == TRUE )) && print -u 2 -- "# Current Seconds: ${MKAT_TIM}" (( VERBOSE == TRUE )) && print -u 2 -- "# Now Seconds....: ${MKAT_NOW}" (( MKAT_ADD = ( MKAT_DAY * 24 * 60 * 60 ) + ( MKAT_HRS * 60 * 60 ) + ( MKAT_MIN * 60 ) )) (( VERBOSE == TRUE )) && print -u 2 -- "# Seconds to add.: ${MKAT_ADD}" (( MKAT_SEC = MKAT_NOW + MKAT_ADD )) (( VERBOSE == TRUE )) && print -u 2 -- "# Future Time....: ${MKAT_SEC}" MKAT_TIM=$( printf "%(%Y%m%d%H%M)T\n" "#${MKAT_SEC}" ) MKAT_TIM="-t ${MKAT_TIM}" fi (( VERBOSE == TRUE )) && print -u 2 -- "# Job Time.......: ${MKAT_TIM}" (( VERBOSE == TRUE )) && print -u 2 -r -- "# print -r -- \"${MKAT_CMD}\" | at ${MKAT_TIM}" if MKAT_OUT=$( print -r -- "${MKAT_CMD}" | at ${MKAT_TIM} 2>&1 ) then if (( MKAT_NUM == TRUE )) then MKAT_JOB="${MKAT_OUT#* }" MKAT_JOB="${MKAT_JOB%% *}" (( VERBOSE == TRUE )) && print -u 2 -- "# Job Number.....: ${MKAT_JOB}" print -- ${MKAT_JOB} else (( VERBOSE == TRUE )) && print -u 2 -- "# Job Results....: ${MKAT_OUT}" print -- ${MKAT_OUT} fi fi return 0 } ################################################################ ################################################################ ################################################################ mkAtJob "${@}"