Crontab is the Linux equivalent of the Window’s Task Scheduler. It can help you set up a task to run automatically at a regular interval. Some of you might already be thinking up various use cases for this functionality. It is most useful to sysadmins who can use it to automate routine maintenance jobs and other tasks.
To help you understand the full potential of Crontab and everything you can do with it, we will go over a handful of practical examples of how to use Crontab to schedule tasks on your Linux system. But first, let’s get a basic understanding of the crontab syntax.
Crontab Syntax – How to Write Crontab Entries
When writing a crontab entry, you have to work with six fields. The first five fields 1-5 are used to specify the date and time when you want the task to run, whereas the sixth field is to define the concerned task.
Here is a basic look of how a Crontab entry should look like:
[minute] [hour] [DayOfMonth] [MonthOfYear] [DayOfWeek] [the script you want to execute]
The acceptable values for each of these fields are as follows:
- Minute: Value can be between 0-59.
- Hour: Value can be between 0-23.
- DayOfMonth: Value can be between 1-31. For months having less than 31 days (say February with 28 days), the extra days will get ignored.
- MonthOfYear: Value can be between 1-12. Here 1 represents January and 12 represents December. The value can also be the first three letters of the month – for January, you can write Jan, and for June, you can write Jun.
- DayOfWeek: Value can be between 0-7. Here both 0 and 7 represents Sunday. Also, the value can be the first three letters of the day – for Monday, you can write, Mon, and for Friday, you can write Fri.
Now that you have a basic understanding of how to define the temporal values, let’s quickly go over setting multiple values and ranges. For this, you can use the following special characters:
- Asterisk (*): this means that the value can match anything.
- Comma (,): use this to define multiple values like 2,4,6 or Tue, Thu, Sat.
- Hyphen (-): this can be used to define a range like 2-6 or Tue-Sat. Here all the numbers between 2 to 6 and all the days from Tuesday to Saturday will be taken.
You can also define multiple ranges by using both comma(,) and hyphen (-) together. For example, if you want to define all the months from January to April and October to December, you can write jan-apr,oct-dec.
How to create or add a New Crontab entry?
So, now that you have a basic idea of how to write Crontab entries let’s see how you can add a task to it. Well, first you need to enter the following command in the terminal:
$ crontab -e
This is going to open a Crontab file in the editor where you can add or edit a job.
By default, it will create the crontab entries for the current user. However, if you want to create a crontab entry for a different user, then you need to use this command instead:
$ crontab -u username -e
The username is just a placeholder where you need to enter the actual username for which you wish to create a new crontab task.
How to view all the Crontab Entries?
Over time, you might have created a bunch of crontab entries on your system. As such, it becomes necessary to review these entries to see which ones you still needed and which ones you can remove.
To list all the crontab entries for the current user, you can use the following command:
$ crontab -l
However, to view the crontab entries of a different user, you will need to use this command instead:
$ crontab -u username -l
So that pretty much covers all the basic syntax and commands you need to use for Crontab. Let’s talk about some practical examples of how you can use Crontab in Linux.
15 Practical Examples of Using Crontab in Linux
Here we will be going over some practical examples of crontab entries that will be useful in real-world circumstances. We will show you the crontab command that you need to write and what it does so you develop a deeper understanding.
1. Schedule a Cron to run daily at a particular time
The following command will run the task /scripts/script.sh, every day at 1 a.m.
0 1 * * * /scripts/script.sh
The asterisk in the third field denotes that it will run every day of the month, the fourth field denotes that it will run every month of the year and the fifth field denotes that it will run every day of the week.
2. Schedule a Cron to run daily
The following command will run the task /scripts/script.sh, daily at midnight.
0 0 * * * /scripts/script.sh
or you can use the command:
Here @daily timestamp substitutes for “0 0 * * *”.
3. Schedule a Cron to run twice a day
The following command will run the task /scripts/script.sh, every day at 1 a.m and 1 p.m.
0 1,13 * * * /scripts/script.sh
Notice that the second field contains two entries separated by a comma to define the two separate time points.
4. Schedule a Cron to run every minute
The following command will run the task /scripts/script.sh, every single minute, repeatedly.
* * * * * /scripts/script.sh
As you can see, all of the five fields that are used to specify the day and time are marked with an asterisk (*).
5. Run every x minutes
The following command will run the task /scripts/script.sh, every 20 minutes.
* /20 * * * /scripts/script.sh
Here we have used /20 in the hour-field, which means the task will run every 20 minutes. If you had used /10 here, it would mean the task would run every 10 minutes.
6. Run every x hours
The following command will run the task /scripts/script.sh, every 6 hours at time 0 minutes.
0 * /6 * * /scripts/script.sh
We have used /6 in the day-field, which is telling it to run the command every 6 hours. Since the minute-field is 0, it will run exactly when the minute hand is at 12(or 0).
7. Schedule a Cron to run every 10 seconds
To execute a command every 10 seconds is not possible using the time parameters along. However, this can be managed by configuring the same Cron twice while including a sleep command.
* * * * * /scripts/script.sh * * * * * sleep 30; /scripts/script.sh
8. Run hourly
The following command will run the task /scripts/script.sh, every hour.
0 * * * * /scripts/script.sh
Like before, you can use the alternative command:
9. Automate a task monthly
The following command will run the task /scripts/script.sh, every month.
0 0 1 * * /scripts/script.sh
or you can use the command:
10. Execute on a selected month
The following command will run the task /scripts/script.sh, only in May. However, it will execute every single minute or every single day of that month.
* * * may * /scripts/script.sh
If you want the task to run on every May, but only on the 1st at 12 a.m, then use this command instead:
0 0 1 may * /scripts/script.sh
11. Schedule a Cron to run on the first Monday of every month
This is a tricky one. By using the time-parameters alone, you can’t create a Cron that executes only on the first Monday of every month. However, you can add a condition in the command field that will help you do it.
0 0 * * sun [$(date +%d) -le 01] && /scripts/script.sh
12. Have it run weekly
The following command will run the task /scripts/script.sh, every week on Mondays at midnight.
0 0 * * mon /scripts/script.sh
or you can use the alternative command as we used before:
The following command will run the task /scripts/script.sh yearly on the first of January at midnight.
0 0 1 1 * /scripts/script.sh
There is also an alternative command for this as well:
14. Run multiple tasks once a day at a particular time
The following command will run these two tasks /scripts/script1.sh and /scripts/script2.sh, every day at midnight.
0 0 * * * /scripts/script1.sh; /scripts/script2.sh
Using the semicolon (;), we can define two tasks instead of one. You can use the procedure to define three, four, … up to n number of functions.
15. Schedule a Cron to run after a system reboot
Sometimes it can be necessary to run a task or a set of tasks every time after a reboot. This is where “@reboot” becomes useful. The following command will run the task /scripts/script.sh on system startup.
So this was our in-depth look at Crontab in Linux and how it can help you automate and schedule repetitive tasks. We hope you found the read to be useful and that it helped you in better understanding Crontab and all that you can do with it. What unique usage did you think of, or applied it to your system with Crontab?