On the Linux system we can schedule a regular task, also knowns as a cron job, using a cron
(crond
) service.
The cron
(crond
) service reads crontab
(cron tables) and executes listed scheduled tasks.
In this article i will show the format of a crontab
and explain how to schedule a cron job in Linux.
You will also find here the most popular examples of cron job schedules, such as every minute cron job, every 5 minutes, every hour, every day (daily cron job) and others.
First of all you have to check that the cron
(crond
) service is running.
Check if the cron
service is running on Ubuntu:
systemctl status cron.service
– or –
service cron status
Check if the crond
service is running on CentOS:
systemctl status crond.service
– or –
service crond status
Also it is necessary to ensure that the cron
(crond
) service is added to autostart.
Cool Tip: Don’t know how to configure a service to run at startup on Ubuntu or CentOS? This is easy! Read more →
As only the cron
(crond
) service is running and configured to start on boot – you can schedule a cron job.
Edit Crontab
Run the following command to edit crontab
of the current user:
$ crontab -e
Edit the crontab
of the user Alice:
$ crontab -u alice -e
View crontab
entries of the current user and of the user Alice:
$ crontab -l $ crontab -u alice -l
Useful Information: The default location of the user’s cron jobs is /var/spool/cron/
directory.
Crontab Format
Each cron job to run has to be defined through a single line indicating when the cron job will be run and what command to run for the cron job.
To define the time you can provide concrete values for minute, hour, day of month, month and day of week.
You can use a wildcard *
in the time fields which means ALL or ANY.
Here is a schema for better understanding of the crontab
format:
.---------------- minute (0 - 59) | .-------------- hour (0 - 23) | | .------------ day of month (1 - 31) | | | .---------- month (1 - 12) OR jan,feb,mar ... | | | | .-------- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue ... | | | | | * * * * * command to be executed
A time tag can be one value, several values, a range or a fractional range.
Examples of a time tag for the hour tag:
Value | Type | Description |
---|---|---|
9 |
One value | Execute at 9am |
6,7,10 |
Multiple values | Execute at 6, 7 and 10am |
6-9 |
Range | Execute every hour between 6-9am (inclusive) |
*/3 |
Fractional | Execute every third hour, i.e. 0 (midnight), 3am, 6am, 9am, etc. |
3-12/3 |
Fractional range | Execute every third hour between 3am and 12am, i.e. 3am, 6am, 9am, 12am |
Cool Tip: Want to become a DevOps engineer? Then you must know Git! This article helps to catch the Git basics really quickly! Read more →
There are several special predefined values which can be used to substitute the cron expression:
Entry | Description | Equiv to |
---|---|---|
@reboot |
Run at boot and reboot only | – |
@yearly |
Run at midnight Jan 1 each year | 0 0 1 1 * |
@annually |
Run at midnight Jan 1 each year | 0 0 1 1 * |
@monthly |
Run at midnight on the first day of each month | 0 0 1 * * |
@weekly |
Run at midnight each Sunday | 0 0 * * 0 |
@daily |
Run at midnight each day | 0 0 * * * |
@midnight |
Run at midnight each day | 0 0 * * * |
@hourly |
Run on the first second of every hour | 0 * * * * |
Cron Job Examples
At the end of this article i would like to introduce several useful examples of cron job schedules.
I look through these examples almost each time when i need to add a cron job to crontab
.
This table helps me a lot and i hope it will help you also.
Here are the most common examples of cron job schedules that can be found in almost any crontab
on Linux:
Schedule | Job |
---|---|
* * * * * |
echo “Run cron job every minute” |
*/5 * * * * |
echo “Run cron job every 5 minutes” |
*/30 * * * * |
echo “Run cron job every 30 minutes” |
0 * * * * |
echo “Run cron job every hour” |
0 */3 * * * |
echo “Run cron job every 3 hours” |
0 13 * * * |
echo “Run cron job every day at 1pm” |
30 2 * * * |
echo “Run cron job every day at 2.30am” |
0 0 * * * |
echo “Run cron job every day at midnight” |
0 0 * * 0 |
echo “Run cron job every Sunday” |
0 0 * * 1 |
echo “Run cron job every Monday” |
0 0 1 * * |
echo “Run cron job every first day of every month” |
0 0 1 1 * |
echo “Run cron job every first of January every year” |