Cron is used for scheduling tasks in Linux. It helps you automate the repeating tasks at ease. The tasks that are performed at pre-scheduled times are called Cron Jobs.
For example, you could create a script for automatically backing up some of your files and run the script as a cron job at a certain interval. We use the crontab
command to edit the cron table and schedule tasks according to our preferences.
In this tutorial, we’ll go through how to run cron jobs every 5, 10, 15, or 30 minutes, as well as the fundamentals of scheduling cron jobs.
If you want an in-depth article that covers all the topics about cron from basics to advanced, check out our tutorial on How to Schedule Cron Jobs in Linux With Crontab.
Table of Contents
Crontab
Before we begin, the most important concept to learn is the crontab, also known as the cron table.
This is a table that we can edit, i.e., add, or remove the tasks we want to run at predefined times. You can access the cron table using the crontab
command. To list the cron table existing for the current user, use the command crontab -l
:
crontab -l
For viewing a specific user’s cron table, use the -u
flag followed by the username:
crontab -u username -l
Be sure to use sudo, especially when you are trying to access another user’s cron table. You can edit the current user’s cron table by using the -e
flag:
crontab -e
Now it’s time to learn how to specify the time in the cron table.
Crontab: Time Parameters and Syntax
The general syntax of crontab is as follows:
* * * * * [path to script/command]
The five asterisks are placeholders for five different time parameters. These parameters are explained below:
* * * * * command | | | | | | | | | +--------------- day of week (0 - 7) | | | +--------------------- month (1 - 12) | | +--------------------------- day of month (1 - 31) | +---------------------------------- hour (0 - 23) +---------------------------------------- minute (0 - 59)
Here is another representation of this:
<minute> <hour> <day-of-month> <month> <day-of-week> command
Replacing asterisks with values is not necessary. If an asterisk is kept instead of a value, that position will have every possible value. This will become clear with the examples below:
10 2 * * * command
Adding this entry to your cron table will run some command
at the 10th minute of the 2nd hour each day through all the months. In other words, some command
will run at 2:10 am daily throughout the year.
As you can guess now, the asterisk means that the specific position will take every value allowed for that position (every day, every month, and every day of the week in this example).
0 18 8 5 * echo "Guess the output" >> output.txt
This line of code will create the output.txt file at 6 pm, 8th May.
Now let’s learn about some operators that will help you create more robust cron table entries.
Cron Table Special Operators
One operator that you’ve already seen is the asterisk. It is a placeholder that can be kept as is, or replaced by a specific value. Some other operators are:
- Comma (,) – Allows specifying multiple values instead of one, each value followed by a comma.
- Hyphen (-) – Allow entering a range of values, starting and ending values are separated with a hyphen.
- Slash (/) – Can be used to assign step values (or incremental values). For example, you can use */10 in the minute’s field to specify that you want the job to run every 10 minutes, or */5 in the hour field means that it will run every 5 hours.
Let’s look at one example with the Slash (/) operator:
*/2 * * * 2 command
Here, something
will run every 2 minutes every Tuesday.
Now, you pretty much know all the things that are required for creating a scheduled task that will run after every certain minute or hour. Let’s see how to actually do it now.
Scheduling Jobs at Every [X] Minutes With Cron
Open up your crontab by using the crontab -e
command:
crontab -e
You will notice every line in the cron table file starts with a #
. This simply means that all the lines are comments, and they will not be executed. You need to add your line with the specified time parameters and the path to the script/command to be executed.
To schedule a job every [X] minutes or hours you need to add the following line at the end of the cron table.
Run command after every [X] minutes:
*/X * * * * command
Run command after every [X] hour:
This is a little different than the previous example. This is because you also need to specify which minute to run the command on. We can use 0
to run the command at the beginning of every Xth hour.
0 */X * * * command
Now let’s see how we can run a script every 5, 10, 15, or 30 minutes. Add the lines of the following section to the end of your cron table.
Run a Cron Job Every 5 minutes
To run a cron job every 5 minutes add the following line to the end of your crontab file:
*/5 * * * * command
Run a Cron Job Every 10 minutes
To run a cron job every 10 minutes add the following line to the end of your crontab file:
*/10 * * * * command
Run a Cron Job Every 15 minutes
To run a cron job every 15 minutes add the following line to the end of your crontab file:
*/15 * * * * command
Run a Cron Job Every 30 minutes
To run a cron job every 30 minutes add the following line to the end of your crontab file:
*/30 * * * * command
Conclusion
In this tutorial, we briefly went over how to schedule tasks at a certain interval. If you want to learn more about cron and how to schedule tasks in detail, check out our in-depth tutorial on scheduling cron jobs.
You can also find out more from the crontab manual using the man crontab
command. We hope this tutorial was helpful for you. Feel free to ask any questions down in the comments, and we’ll get back to you as soon as possible.