Automate tasks with crontab


- Setup

yum install cronie

- Start crontab and automatically run every reboot:

service crond start
chkconfig crond on

- Some commonly used commands:

crontab -e: create or edit crontab file 
crontab -l: show crontab file
crontab -r: remove crontab file

- Structure of crontab file has 5 timed fields, finally the command will be run periodically, the structure is as follows:

*     *     *     *     *     command to be executed
-     -     -     -     -
|     |     |     |     |
|     |     |     |     +----- day of week (0 - 6) (Sunday=0)
|     |     |     +------- month (1 - 12)
|     |     +--------- day of month (1 - 31)
|     +----------- hour (0 - 23)
+------------- min (0 - 59)


– Run the script every 30 minutes

0,30 * * * * command

– Run the script every 15 minutes:

0,15,30,45 * * * * command

– Run script at 3am every day:

0 3 * * * command
@reboot  Run once every reboot
@yearly  Run once a year    "0 0 1 1 *"
@annually (Similar to @yearly)
@monthly Run once a month  "0 0 1 * *"
@weekly  Run once a week  "0 0 * * 0"
@daily  Run once a day    "0 0 * * *"
@midnight (Similar to @daily)
@hourly  Run once per hour    "0 * * * *"

- View log

tail /var/spool/mail/[username]
vi /var/spool/mail/[username]

- Fix error command not found /bash/sh

SHELL=/bin/bash