Differences
This shows you the differences between two versions of the page.
tech:others:cron_to_schedule_specific_recurring_days [2014/11/15 09:13] |
tech:others:cron_to_schedule_specific_recurring_days [2014/11/15 09:13] (current) |
||
---|---|---|---|
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 ==== | ||
+ | <code> | ||
+ | 01 17 * * 5 /home/xxxxxx/reminder/rem1.bash | ||
+ | </code> | ||
+ | |||
+ | ==== 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 "$TODAYDATE - $EVENWEEK" | ||
+ | # | ||
+ | # If Odd week then Send Notification | ||
+ | if [ "$EVENWEEK" = "0" ]; then | ||
+ | echo "Odd Friday ..." | /usr/bin/mailx -s "Message here" "4445556666@mobile.mycingular.com" | ||
+ | fi | ||
+ | # | ||
+ | exit | ||
+ | </code> | ||
+ | |||
+ | |||
+ | ===== 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 ==== | ||
+ | <code> | ||
+ | 14 08 * * 6 /home/xxxxxx/reminder/rem2.bash | ||
+ | </code> | ||
+ | |||
+ | ==== 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 [ "$WEEKNUM" = "0" ]; then | ||
+ | echo "1st Sat" | /usr/bin/mailx -s "message" "4445556666@mobile.mycingular.com" | ||
+ | fi | ||
+ | # | ||
+ | exit | ||
+ | </code> | ||
+ | |||
+ | ===== Other considerations ===== | ||
+ | For the 1st Saturday weekend my initial "reaction" was to just do a crontab such as this: | ||
+ | <code> | ||
+ | 14 08 1-7 * 6 /home/xxxxxx/reminder/rem2.bash | ||
+ | </code> | ||
+ | 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. | ||
+ | |||