Understand Cron Jobs In 5 Minutes
In Linux, the crontab command is used to schedule execution of commands at certain time intervals whether it be hourly, daily, monthly or every x amount of minutes. This article is designed to show you the simple way of understanding crontab.
Understanding the fields:
# (Use to post in the top of your crontab) # ------------- minute (0 - 59) # | ----------- hour (0 - 23) # | | --------- day of month (1 - 31) # | | | ------- month (1 - 12) # | | | | ----- day of week (0 - 6) (Sunday=0) # | | | | | # * * * * * command to be executed
We can see that if you include the field for command to be executed there are a total of six fields that can be used when setting up a cron job.
Setting up a cron job:
The first thing we want to do is open up the crontab. To do this, we need to execute the following command:
root@foo~:# crontab -e
This will open crontab in the default editor on your system, which is usually Vi. To modify this editor, as root, execute the following command:
root@foo~:# export VISUAL=’nano -w’
Once inside the editor, you will want to refer to the fields above in order to schedule a cron job for the appropriate time. Here are some examples:
*/5 * * * * /home/adam/script.sh will execute script.sh every 5 minutes. This will set crontab every 5 minutes.
59 23 * * 1-5 /home/adam/script.sh will execute script.sh every day, monday through friday, at 11:59 p.m.
0 0 * * 0 /home/adam/script.sh will execute script.sh once a week. You could also specify @weekly instead of 0 0 * * 0.
0 23 1 * * /home/adam/script.sh will execute script.sh once a month, on the first, at 11:00 PM. You could also specify @monthly in place of 0 23 1 * *.
You should now have a firm understanding of setting up cron jobs for your Linux system. To learn more about crontab, check out the following sites:
Popularity: 36% [?]
Filed under: Linux
Like this post? Subscribe to my RSS feed and get loads more!