Crontab - Crontab is where you configure what interval and which script to execute.
To see crontab entry.
| [root@myserver]# crontab -l |
To edit crontab entry.
| [root@myserver]# crontab -e |
How to schedule cron to execute a script.
You have to edit the crontab to the below format in order for cron to execute your script at your desired time.
| * * * * * /root/script.sh | | | | | | | | | | | | | | |_____ day of week (0 - 6) (0 to 6 are Sun to Sat) | | | |_______ month (1 - 12) | | |_________ day of month (1 - 31) | |___________ hour (0 - 23) |_____________ min (0 - 59) |
Examples.
To execute script /root/script.sh every Saturday 1am.
| 0 1 * * 5 /root/script.sh |
To execute script /root/script.sh on every weekdays at 2am.
| 0 2 * * 1-5 /root/script.sh |
To execute script /root/script.sh every 15min daily.
| 0,15,30,45 * * * * /root/script.sh |
To execute script /root/script.sh every 15min daily and output the output of /root/script.sh to /root/script_output.txt.
| 0,15,30,45 * * * * /root/script.sh >> /root/script_output.txt 2>&1 |
the standard output is stored to the file stated which in this case is /root/script_output.txt.
To execute script /root/script.sh every 15min daily and tell it not to show any output.
| 0,15,30,45 * * * * /root/script.sh >> /dev/null 2>&1 |
Default location of crontab for all users in most Unix operating system.
The default location where crontab is stored is at /var/spool/cron*
For Linux it is at /var/spool/cron/
For Solaris, AIX and HP-UX it is at /var/spool/cron/crontabs/