Cron Jobs
- Cron – A time based job scheduling service
- Crontab -A program to create, read, update, and delete your schedules.
- Use cron to schedule and automate tasks.
A crontab or table is used to specify when commands will be executed by cron. Any line starting with “#” within the editor will be ignored, as It’s for comments.
* * * * * Command
| | | | |
| | | | + — Day of the week (0-6)
| | | + —- Month of the Year (1-12)
| | + ——–Day of the month (1-31)
| + ————-Hour (0-23)
+ —————–Minute (0 – 59)
Example : # Run every monday at 7 AM
0 7 * * 1 /opt/sales/bin/weekly-report
| | | | |
| | | | + — Day of the week (0-6)
| | | + —- Month of the Year (1-12)
| | + ——–Day of the month (1-31)
| + ————-Hour (0-23)
+ —————–Minute (0 – 59)
If any output is generated by a cron job, it will be mailed by you.
Example: #Run at 2:00 every day and send the output to a file log
0 2 * * * /root/backupdb > /tmp/db.log 2>&1
If you want to run a command to run every thirty minutes simply use a 0,30 or 0/2 in the minutes tab. If you want to run a command every 5 minutes at the top of an hour, use 0-4 in the minutes tab.
Examples:
0 0 1 1 * yearly
0 0 1 * * monthly
0 0 * * 0 weekly
0 0 * * * daily
0 * * * * hourly
*/15 * * * * every 15 mins
0 */6 * * * every 6 hours
@reboot every reboot
Quickly adding a task to crontab;
echo "0 */6 * * * echo Follow the white rabbit..." | crontab
After the quick add, you will see the following:
root@ubuntu:~# echo "0 */6 * * * echo Follow the White Rabbit..." | crontab
root@ubuntu:~# crontab -l
0 */6 * * * echo Follow the White Rabbit...
Using the Crontab Command:
# crontab file – Install a new crontab from file
# crontab -l – List your cron jobs
# crontab -e – Edit your cron jobs, will take you to the editor
# crontab -r – Remove all of your cron jobs
For additional information, review the man page or use crontab –help