linux_wiki:cron

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
linux_wiki:cron [2017/09/03 17:50]
billdozor [Automation Example]
linux_wiki:cron [2019/05/25 23:50] (current)
Line 144: Line 144:
  
   * Cron entry: Execute a date checker script every Tuesday   * Cron entry: Execute a date checker script every Tuesday
-    * Create the system wide cron entry (/etc/cron.d/automated_job)<code bash cron-automated-jobs># .---------------- minute (0 - 59)+    * Create the system wide cron entry (/etc/cron.d/automated_jobs)<code bash cron-automated-jobs># .---------------- minute (0 - 59)
 # |  .------------- hour (0 - 23) # |  .------------- hour (0 - 23)
 # |  |  .---------- day of month (1 - 31) # |  |  .---------- day of month (1 - 31)
Line 193: Line 193:
 else else
   echo ">> NO-EXEC: Today is NOT the first,second, or third Tue of the Month. Will NOT execute script(${script})." >> ${log_file}   echo ">> NO-EXEC: Today is NOT the first,second, or third Tue of the Month. Will NOT execute script(${script})." >> ${log_file}
 +fi
 +
 +echo -e "==== Log Ended: $(date) ====\n" >> ${log_file}
 +</code>
 +
 +==== Other Date Check Examples ====
 +
 +A few other examples of date checker scripts with relative days before/after.
 +
 +\\
 +**7 Days before the first Tuesday of the month**<code bash>#!/bin/bash
 +# Title: automated-job-check.sh
 +# Description: Determine if an automated job should be run
 +
 +# Script to execute:
 +script="/automation/scripts/automated-job.py"
 +
 +# Log file
 +log_file="/var/log/automation/automated-job-check_$(date +%Y%m).log"
 +
 +echo "==== Log Started: $(date) ====" >> ${log_file}
 +
 +# If today is Tuesday AND 7 days from now is >=1 and <=7(first Tue)
 +if [[ "$(date '+%a')" == "Tue" ]] && [[ $(date +%-d -d "+7 days") -ge 1 ]]; then
 +  if [[ $(date +%-d -d "+7 days") -le 7 ]]; then
 +    echo ">> OK: Seven days from now is the first Tue of the Month; execute script(${script})." >> ${log_file}
 +    ${script} 2>&1 >> ${log_file}
 +  else
 +    echo ">> NO-EXEC: Seven days from now is NOT the first Tue of the Month. Will NOT execute script(${script})." >> ${log_file}
 +  fi
 +else
 +  echo ">> NO-EXEC: Seven days from now is NOT the first Tue of the Month. Will NOT execute script(${script})." >> ${log_file}
 +fi
 +
 +echo -e "==== Log Ended: $(date) ====\n" >> ${log_file}
 +</code>
 +
 +\\
 +**2 Days after the third Tuesday of the month**<code bash>#!/bin/bash
 +# Title: automated-job-check.sh
 +# Description: Determine if an automated job should be run
 +
 +# Script to execute
 +script="/automation/scripts/automated-job.py"
 +
 +# Log file
 +log_file="/var/log/automation/automated-job-check_$(date +%Y%m).log"
 +
 +echo "==== Log Started: $(date) ====" >> ${log_file}
 +
 +# If today is Thursday AND two days ago was >=15 and <=21(third Tue)
 +if [[ "$(date '+%a')" == "Thu" ]] && [[ $(date +%-d -d "-2 days") -ge 15 ]]; then
 +  if [[ $(date +%-d -d "-2 days") -le 21 ]]; then
 +    echo ">> OK: Two days ago was the third Tue of the Month; execute script(${script})." >> ${log_file}
 +    ${script} 2>&1 >> ${log_file}
 +  else
 +    echo ">> NO-EXEC: Two days ago was NOT the third Tue of the Month. Will NOT execute script(${script})." >> ${log_file}
 +  fi
 +else
 +  echo ">> NO-EXEC: Two days ago was NOT the third Tue of the Month. Will NOT execute script(${script})." >> ${log_file}
 fi fi
  
  • linux_wiki/cron.1504475404.txt.gz
  • Last modified: 2019/05/25 23:50
  • (external edit)