A Beginners Guide To Cron Jobs
A Beginners Guide To Cron Jobs
Cron is one of the most useful utility that you can find in any Unix-like operating
system. It is used to schedule commands at a specific time. These scheduled
commands or tasks are known as “Cron Jobs”. Cron is generally used for running
scheduled backups, monitoring disk space, deleting files (for example log files)
periodically which are no longer required, running system maintenance tasks and a lot
more. In this brief guide, we will see the basic usage of Cron Jobs in Linux.
Just memorize the cron job format or print the following illustration and keep it in
your desk.
In the above picture, the asterisks refers the specific blocks of time.
To display the contents of the crontab file of the currently logged in user:
$ crontab -l
$ crontab -e
If it is the first time, you will be asked to choose an editor to edit the cron jobs.
Choose any one that suits you. Here it is how a sample crontab file looks like.
In this file, you need to add your cron jobs one by one.
$ crontab -u ostechnix -e
1. To run a cron job at every minute, the format should be like below.
* * * * * <command-to-execute>
For example if the time is 10:00, the next job will run at 10:01, 10:02, 10:03 and so
on.
2. To run cron job at every 5th minute, add the following in your crontab file.
*/5 * * * * <command-to-execute>
For example if the time is 10:00, the next job will run at 10:05, 10:10, 10:15 and so
on.
3. To run a cron job at every quarter hour (i.e every 15th minute), add this:
*/15 * * * * <command-to-execute>
For example if the time is 10:00, the next job will run at 10:15, 10:30, 10:45 and so
on.
30 * * * * <command-to-execute>
For example if the time is 10:00, the next job will run at 10:30, 11:30, 12:30 and so
on.
5. You can also define multiple time intervals separated by commas. For example, the
following cron job will run three times every hour, at minute 0, 5 and 10:
0,5,10 * * * * <command-to-execute>
6. Run a cron job every half hour i.e at every 30th minute:
*/30 * * * * <command-to-execute>
For example if the time is now 10:00, the next job will run at 10:30, 11:00, 11:30 and
so on.
0 * * * * <command-to-execute>
For example if the time is now 10:00, the next job will run at 11:00, 12:00, 12:00 and
so on.
0 */2 * * * <command-to-execute>
For example if the time is now 10:00, the next job will run at 12:00.
0 0 * * * <command-to-execute>
0 3 * * * <command-to-execute>
0 0 * * SUN <command-to-execute>
Or,
0 0 * * 0 <command-to-execute>
12. Run a job on every day-of-week from Monday through Friday i.e every
weekday:
0 0 * * 1-5 <command-to-execute>
0 0 1 * * <command-to-execute>
15 16 1 * * <command-to-execute>
15. Run a job at every quarter i.e on day-of-month 1 in every 3rd month:
0 0 1 */3 * <command-to-execute>
5 0 * 4 * <command-to-execute>
0 0 1 */6 * <command-to-execute>
This cron job will start at 00:00 on day-of-month 1 in every 6th month.
0 0 1 1 * <command-to-execute>
19. To run a job every time the server is rebooted, add this line in your crontab file.
@reboot <command-to-execute>
$ crontab -r
$ man crontab
At this stage, you might have a basic understanding of what is Crontab and how to
create and run a cron job in Unix-like systems.