Differences
This shows you the differences between two versions of the page.
linux_wiki:cron [2017/09/03 18:00] billdozor [Automation Example] |
linux_wiki:cron [2019/05/25 23:50] |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Cron ====== | ||
- | |||
- | **General Information** | ||
- | |||
- | Automating tasks with cron (the daemon that executes scheduled commands). | ||
- | |||
- | **Checklist** | ||
- | * Distro(s): Any | ||
- | |||
- | ---- | ||
- | |||
- | ====== User Crontabs ====== | ||
- | |||
- | Individual users can create/edit their own cron jobs. | ||
- | |||
- | \\ | ||
- | List your user's crontab jobs | ||
- | <code bash> | ||
- | crontab -l | ||
- | </ | ||
- | |||
- | \\ | ||
- | Edit your crontab | ||
- | <code bash> | ||
- | crontab -e | ||
- | </ | ||
- | |||
- | \\ | ||
- | User crontabs are stored at: | ||
- | * / | ||
- | |||
- | ===== Files That Control Crontab Access ===== | ||
- | |||
- | * / | ||
- | * / | ||
- | |||
- | About these files: | ||
- | * One username per line | ||
- | * Root can always use crontab, regardless of file existence below | ||
- | |||
- | ^ cron.allow | ||
- | | file exists | ||
- | | no file | file exists | ||
- | | file exists | ||
- | | no file | no file | Only root is allowed to use crontab | ||
- | |||
- | ---- | ||
- | |||
- | ===== Format of User Crontabs ===== | ||
- | |||
- | For any below, asterisk means all values. | ||
- | <code bash> | ||
- | # .---------------- minute (0 - 59) | ||
- | # | .------------- hour (0 - 23) | ||
- | # | | .---------- day of month (1 - 31) | ||
- | # | | | .------- month (1 - 12) OR jan, | ||
- | # | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun, | ||
- | # | | | | | | ||
- | # * * * * * command to be executed | ||
- | </ | ||
- | |||
- | ---- | ||
- | |||
- | ===== Crontab Examples ===== | ||
- | |||
- | Executes script.sh at 0600, | ||
- | <code bash> | ||
- | 0,30 6,12 * * * / | ||
- | </ | ||
- | |||
- | Executes script.sh every minute. | ||
- | <code bash> | ||
- | * * * * * / | ||
- | </ | ||
- | |||
- | Executes script.sh every 5 minutes. | ||
- | <code bash> | ||
- | */5 * * * * / | ||
- | </ | ||
- | |||
- | Executes script.sh on system startup. | ||
- | <code bash> | ||
- | @reboot / | ||
- | </ | ||
- | |||
- | ---- | ||
- | |||
- | ====== System Cron Jobs ====== | ||
- | |||
- | To run jobs in the system directories, | ||
- | |||
- | * / | ||
- | * / | ||
- | * / | ||
- | * / | ||
- | * / | ||
- | * / | ||
- | |||
- | It is recommended to drop scripts into one of the /etc/cron.* directories instead of editing the system wide crontab file. | ||
- | |||
- | All formats are bash scripts, except for the / | ||
- | |||
- | For example, / | ||
- | <code bash> | ||
- | # .---------------- minute (0 - 59) | ||
- | # | .------------- hour (0 - 23) | ||
- | # | | .---------- day of month (1 - 31) | ||
- | # | | | .------- month (1 - 12) OR jan, | ||
- | # | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun, | ||
- | # | | | | | | ||
- | # * * * * * user-name | ||
- | |||
- | # Rsync the crashplan backups to another usb drive for redundancy | ||
- | 0 2,14 * * * root / | ||
- | </ | ||
- | The above executes at 0200 and 1400, runs as the user root, the script specified. | ||
- | |||
- | ---- | ||
- | |||
- | ====== Cron Keywords ====== | ||
- | |||
- | Keywords that can be used in place of a specific time/date schedule. | ||
- | |||
- | * @reboot - Run once at start up. | ||
- | * @yearly - Run once a year. | ||
- | * @annually - Same as yearly. | ||
- | * @monthly - Run once a month. | ||
- | * @weekly - Run once a week. | ||
- | * @daily - Run once a day. | ||
- | * @midnight - Same as daily. | ||
- | * @hourly - Run once an hour. | ||
- | |||
- | ---- | ||
- | |||
- | ====== Cron - Relative Days ====== | ||
- | |||
- | Cron is great at being specific about minutes, hours, dates, months, and days of the week, but is not able to handle relative days. | ||
- | * Relative days could come in handy when you need a job to execute on something such as the 1st, 2nd and 3rd Tuesday of every month. | ||
- | * In order to accomplish this, a combination of cron and a date checker script is used. | ||
- | |||
- | ===== Automation Example ===== | ||
- | |||
- | 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 (/ | ||
- | # | .------------- hour (0 - 23) | ||
- | # | | .---------- day of month (1 - 31) | ||
- | # | | | .------- month (1 - 12) OR jan, | ||
- | # | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun, | ||
- | # | | | | | | ||
- | # * * * * * user-name | ||
- | |||
- | #Every Tuesday at 0800: Determine if an automated job should be run | ||
- | #-Target: 1st,2nd,3rd Tues of month | ||
- | 00 08 * * tue root / | ||
- | </ | ||
- | |||
- | * Date checker script: Determine if the actual automated program should run and what arguments to send it. | ||
- | * Create the date checker script (/ | ||
- | # Title: automated-job-check.sh | ||
- | # Description: | ||
- | |||
- | # Script to execute: See 'if statement' | ||
- | script="/ | ||
- | |||
- | # Log file | ||
- | log_file="/ | ||
- | |||
- | echo "==== Log Started: $(date) ====" >> ${log_file} | ||
- | |||
- | # If today is Tuesday AND today is >=1 and < | ||
- | if [[ " | ||
- | |||
- | # First Tue: Development Environment | ||
- | if [[ $(date +%-d) -le 7 ]]; then | ||
- | echo ">> | ||
- | ${script} --group all_dev 2>&1 >> ${log_file} | ||
- | |||
- | # Second Tue: Test Environment | ||
- | elif [[ $(date +%-d) -le 14 ]]; then | ||
- | echo ">> | ||
- | ${script} --group all_test 2>&1 >> ${log_file} | ||
- | |||
- | # Third Tue: Production Environment | ||
- | elif [[ $(date +%-d) -le 21 ]]; then | ||
- | echo ">> | ||
- | ${script} --group all_prod 2>&1 >> ${log_file} | ||
- | |||
- | else | ||
- | echo ">> | ||
- | fi | ||
- | else | ||
- | echo ">> | ||
- | fi | ||
- | |||
- | echo -e "==== Log Ended: $(date) ====\n" | ||
- | </ | ||
- | |||
- | ==== Other Date Check Examples ==== | ||
- | |||
- | A few other examples of date checker scripts with relative days before/ | ||
- | |||
- | \\ | ||
- | **7 Days before the first Tuesday of the month**< | ||
- | # Title: automated-job-check.sh | ||
- | # Description: | ||
- | |||
- | # Script to execute: | ||
- | script="/ | ||
- | |||
- | # Log file | ||
- | log_file="/ | ||
- | |||
- | echo "==== Log Started: $(date) ====" >> ${log_file} | ||
- | |||
- | # If today is Tuesday AND 7 days from now is >=1 and < | ||
- | if [[ " | ||
- | if [[ $(date +%-d -d "+7 days") -le 7 ]]; then | ||
- | echo ">> | ||
- | ${script} 2>&1 >> ${log_file} | ||
- | else | ||
- | echo ">> | ||
- | fi | ||
- | else | ||
- | echo ">> | ||
- | fi | ||
- | |||
- | echo -e "==== Log Ended: $(date) ====\n" | ||
- | </ | ||
- | |||
- | \\ | ||
- | **2 Days after the third Tuesday of the month**< | ||
- | # Title: automated-job-check.sh | ||
- | # Description: | ||
- | |||
- | # Script to execute | ||
- | script="/ | ||
- | |||
- | # Log file | ||
- | log_file="/ | ||
- | |||
- | echo "==== Log Started: $(date) ====" >> ${log_file} | ||
- | |||
- | # If today is Thursday AND two days ago was >=15 and < | ||
- | if [[ " | ||
- | if [[ $(date +%-d -d "-2 days") -le 21 ]]; then | ||
- | echo ">> | ||
- | ${script} 2>&1 >> ${log_file} | ||
- | else | ||
- | echo ">> | ||
- | fi | ||
- | else | ||
- | echo ">> | ||
- | fi | ||
- | |||
- | echo -e "==== Log Ended: $(date) ====\n" | ||
- | </ | ||
- | |||
- | ---- | ||