Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # Important to format it as HHMM using 24 hour clock. So hours are from 00 to 23.
- STARTTIME="0500"
- STOPTIME="0700"
- # Wait time between jobs. Must be in seconds. MAX must be less time than the difference between START and STOP.
- MAXWAITTIME="3600"
- MINWAITTIME="1800"
- # Because long version hasn't run yet, we use 0. it will change to 1 when it has run.
- HASRUN=0
- if [[ ${STARTTIME} -gt ${STOPTIME} ]]; then
- STARTTIME="${STARTTIME}"
- else
- STARTTIME="1${STARTTIME}"
- fi
- STOPTIME="1${STOPTIME}"
- # {other declarations and functions go here}
- function WhichJob {
- CURRENTTIME="$(date +%H%M)"
- if ( StartAfterStop && [[ "${CURRENTTIME}" -lt ${STARTTIME} ]] ) || ! StartAfterStop; then
- CURRENTTIME="1${CURRENTTIME}"
- fi
- if HasRun && [[ ${CURRENTTIME} -le ${STOPTIME} ]] && [[ ${CURRENTTIME} -ge ${STARTTIME} ]]; then
- HASRUN=1
- LongVersion
- elif ! HasRun && [[ ${CURRENTTIME} -le ${STOPTIME} ]] && [[ ${CURRENTTIME} -ge ${STARTTIME} ]]; then
- HASRUN=1
- ShortVersion
- else
- HASRUN=0
- ShortVersion
- fi
- }
- function HasRun {
- # Use return to send the contents of ${HASRUN} to the if statement. 0 is true and 1 is false.
- return ${HASRUN} # We return the HASRUN variable as an error code.
- }
- function StartAfterStop {
- # Use return to send a 0 or 1 to the if statement. 0 is true and 1 is false.
- if [[ ${STARTTIME} -lt "10000" ]]; then
- return 0
- else
- return 1
- fi
- }
- function LongVersion {
- echo "Run Long Job"
- }
- function ShortVersion {
- echo "Run Short Job"
- }
- while true; do
- SECONDS=0
- WhichJob
- RUNTIME=${SECONDS}
- if [[ ${RUNTIME} -gt ${MAXWAITTIME} ]] || [[ $((${MAXWAITTIME}-${RUNTIME})) -lt ${MINWAITTIME} ]]; then
- sleep ${MINWAITTIME}
- else
- sleep ${MAXWAITTIME}
- fi
- done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement