no way to compare when less than two revisions
Differences
This shows you the differences between two versions of the page.
| — | tech:others:cron_to_schedule_specific_recurring_days [2014/11/15 15:13] (current) – created - external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== Cronjob to schedule specific recurring days ====== | ||
| + | If you want to run schedule for a certain recurring time such as the 1st Saturday of the month, etc then cron does not have the facility to do that. However the underlying job can have a filter to only trigger for those specific conditions. I have two conditions listed below and the script to go with them. | ||
| + | |||
| + | ===== Setup cron for 1st, 3rd and 5th weekends ===== | ||
| + | This script will send a message to my phone Each **Friday** evening at **5:01 pm** on the **odd weekends** to my cell phone. | ||
| + | ==== Cron Entry ==== | ||
| + | < | ||
| + | 01 17 * * 5 / | ||
| + | </ | ||
| + | |||
| + | ==== rem1.bash script ==== | ||
| + | <code bash> | ||
| + | #!/bin/bash | ||
| + | # Script to send notification to phone on 1st, 2nd, 3rd Week (Friday as defined in cron) | ||
| + | # Get the date number (1-31) | ||
| + | TODAYDATE=$(date +%d) | ||
| + | # Subtract 1 so 0-6 is 1st week and so on | ||
| + | DATEM1=$(($TODAYDATE - 1)) | ||
| + | # Divide by 7 to get the Week # from 0 to 4 (5 weeks) | ||
| + | WEEKNUM=$(($DATEM1 / 7)) | ||
| + | # Divide by 2 to get odd/even week | ||
| + | EVENWEEK=$(($WEEKNUM % 2)) | ||
| + | #echo " | ||
| + | # | ||
| + | # If Odd week then Send Notification | ||
| + | if [ " | ||
| + | echo "Odd Friday ..." | / | ||
| + | fi | ||
| + | # | ||
| + | exit | ||
| + | </ | ||
| + | |||
| + | |||
| + | ===== Setup cron for 1st Saturday | ||
| + | This script will send a message to my phone Each **Saturday** morning at **8:14 am** on the **1st (Sat) weekend** to my cell phone. | ||
| + | ==== Cron Entry ==== | ||
| + | < | ||
| + | 14 08 * * 6 / | ||
| + | </ | ||
| + | |||
| + | ==== rem2.bash script ==== | ||
| + | <code bash> | ||
| + | #!/bin/bash | ||
| + | # Script to send notification the 1st sat | ||
| + | # Get the date number (1-31) | ||
| + | TODAYDATE=$(date +%d) | ||
| + | # Subtract 1 so 0-6 is 1st week and so on | ||
| + | DATEM1=$(($TODAYDATE - 1)) | ||
| + | # Divide by 7 to get the Week # from 0 to 4 (5 weeks) | ||
| + | WEEKNUM=$(($DATEM1 / 7)) | ||
| + | # If 1st week then Send Notification | ||
| + | if [ " | ||
| + | echo "1st Sat" | / | ||
| + | fi | ||
| + | # | ||
| + | exit | ||
| + | </ | ||
| + | |||
| + | ===== Other considerations ===== | ||
| + | For the 1st Saturday weekend my initial " | ||
| + | < | ||
| + | 14 08 1-7 * 6 / | ||
| + | </ | ||
| + | However cron interprets this as trigger on days 1-7 **plus** Saturdays! So that won't quite work. | ||
| + | |||
| + | This has been very helpful to do notification for odd, even weekends, specific week such as 1st, 2nd, 3rd, 4th or 5th weekends and specific day of a specific weekend such as 1st Saturday, 3rd Thursday, etc. | ||
| + | |||