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:40]
billdozor
linux_wiki:cron [2019/05/25 23:50] (current)
Line 139: Line 139:
   * In order to accomplish this, a combination of cron and a date checker script is used.   * In order to accomplish this, a combination of cron and a date checker script is used.
  
-Automation Example1st, 2nd, 3rd Tuesday of every month +===== Automation Example ===== 
-  Create the system wide cron entry (/etc/cron.d/automated_job)<code bash># .---------------- minute (0 - 59)+ 
 +In this example, we want a script to execute on the 1st, 2nd, and 3rd Tuesday of every month, with different arguments depending upon which Tuesday it is. 
 + 
 +  * Cron entry: Execute a date checker script every Tuesday 
 +    * 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 152: Line 156:
 00 08 * * tue  root  /automation/automated-job-check.sh 00 08 * * tue  root  /automation/automated-job-check.sh
 </code> </code>
-  - Create the date checker script (/automation/automated-job-check.sh)<code bash>#!/bin/bash+ 
 +  * Date checker script: Determine if the actual automated program should run and what arguments to send it. 
 +    * Create the date checker script (/automation/automated-job-check.sh)<code bash automated-job-check.sh>#!/bin/bash
 # Title: automated-job-check.sh # Title: automated-job-check.sh
 # Description: Determine if an automated job should be run # Description: Determine if an automated job should be run
Line 166: Line 172:
 # If today is Tuesday AND today is >=1 and <=21(1st,2nd,3rd Tue) # If today is Tuesday AND today is >=1 and <=21(1st,2nd,3rd Tue)
 if [[ "$(date '+%a')" == "Tue" ]] && [[ $(date +%-d) -ge 1 ]]; then if [[ "$(date '+%a')" == "Tue" ]] && [[ $(date +%-d) -ge 1 ]]; then
 +
   # First Tue: Development Environment   # First Tue: Development Environment
   if [[ $(date +%-d) -le 7 ]]; then   if [[ $(date +%-d) -le 7 ]]; then
Line 186: 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.1504474851.txt.gz
  • Last modified: 2019/05/25 23:50
  • (external edit)