Advertisement
thisiszeev

Untitled

Dec 19th, 2024
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.77 KB | Source Code | 0 0
  1. #!/bin/bash
  2.  
  3. # Important to format it as HHMM using 24 hour clock. So hours are from 00 to 23.
  4. STARTTIME="0500"
  5. STOPTIME="0700"
  6.  
  7. # Wait time between jobs. Must be in seconds. MAX must be less time than the difference between START and STOP.
  8. MAXWAITTIME="3600"
  9. MINWAITTIME="1800"
  10.  
  11. # Because long version hasn't run yet, we use 0. it will change to 1 when it has run.
  12. HASRUN=0
  13.  
  14. if [[ ${STARTTIME} -gt ${STOPTIME} ]]; then
  15.   STARTTIME="${STARTTIME}"
  16. else
  17.   STARTTIME="1${STARTTIME}"
  18. fi
  19.  
  20. STOPTIME="1${STOPTIME}"
  21.  
  22. # {other declarations and functions go here}
  23.  
  24. function WhichJob {
  25.   CURRENTTIME="$(date +%H%M)"
  26.   if ( StartAfterStop && [[ "${CURRENTTIME}" -lt ${STARTTIME} ]] ) || ! StartAfterStop; then
  27.     CURRENTTIME="1${CURRENTTIME}"
  28.   fi
  29.   if HasRun && [[ ${CURRENTTIME} -le ${STOPTIME} ]] && [[ ${CURRENTTIME} -ge ${STARTTIME} ]]; then
  30.     HASRUN=1
  31.     LongVersion
  32.   elif ! HasRun && [[ ${CURRENTTIME} -le ${STOPTIME} ]] && [[ ${CURRENTTIME} -ge ${STARTTIME} ]]; then
  33.     HASRUN=1
  34.     ShortVersion
  35.   else
  36.     HASRUN=0
  37.     ShortVersion
  38.   fi
  39. }
  40.  
  41. function HasRun {
  42.   # Use return to send the contents of ${HASRUN} to the if statement. 0 is true and 1 is false.
  43.   return ${HASRUN} # We return the HASRUN variable as an error code.
  44. }
  45.  
  46. function StartAfterStop {
  47.   # Use return to send a 0 or 1 to the if statement. 0 is true and 1 is false.
  48.   if [[ ${STARTTIME} -lt "10000" ]]; then
  49.     return 0
  50.   else
  51.     return 1
  52.   fi
  53. }
  54.  
  55. function LongVersion {
  56.   echo "Run Long Job"
  57. }
  58.  
  59. function ShortVersion {
  60.   echo "Run Short Job"
  61. }
  62.  
  63. while true; do
  64.   SECONDS=0
  65.   WhichJob
  66.   RUNTIME=${SECONDS}
  67.   if [[ ${RUNTIME} -gt ${MAXWAITTIME} ]] || [[ $((${MAXWAITTIME}-${RUNTIME})) -lt ${MINWAITTIME} ]]; then
  68.     sleep ${MINWAITTIME}
  69.   else
  70.     sleep ${MAXWAITTIME}
  71.   fi
  72. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement