Automation is one of the key aspects of any system, whether physical or virtual private server (VPS). If set up correctly, automation can save hundreds of valuable user hours and make the overall workflow much more efficient. One of the main job scheduling tools is cron.
In this tutorial, we’ll cover the crontab syntax, introduce the differences between cron job, cron, and crontab, and provide some useful cron job use cases for a Linux-based operating system.
What is Crontab and a Cron Job?
Regarding cron jobs, three terms can be highlighted:
cron daemon (crond) or simply cron – an executable that allows users to perform tasks and have them automatically run at a specific time.
Scheduled task – any task scheduled by a user using cron is called Scheduled task.
What is the Crontab Syntax?
Cron board Where crontab is a file containing all schedules of cron jobs that a user wants to run regularly. Commands are written one per line and instruct the cron daemon to run a task at a specific time.
Crontab format and values
For the cron daemon to understand the instructions correctly, the correct crontab syntax must be used. The Crontab syntax consists of five fields. Each can be populated with one of the values shown in the following table:
|day of the month||1-31|
|Day of the week||0-6. 0 represents Sunday. In some systems, a value of 7 represents Sunday instead|
|Ordered||Command to execute|
In addition to any crontab values, some special characters must be remembered:
|* (asterisk)||Select all possible values in a field||Place * in the hour field to run the task every hour|
|(comma)||A comma is used to separate multiple values||0.3.5 in the day of the week field will make the task run on Sunday and Wednesday|
|– (hyphen)||Used to define a range of values||10-15 in the day of the month field will run the task from the 10th to the 15th of the month|
|/ (separator)||A separator is used to divide the values||*/10 in the hour field will make the task run every 10 hours|
|L||Used in the day of the month Where day of the week the fields||1L in the day of the week field will run the task on the last Monday of a given month|
|O||W is used to determine the nearest day of the week||0W in the day of the month field will run the task on the closest Sunday of a given month|
|# (hash)||Used to determine the day of the week||2#3 in the day of the month field will have the task run on the third Tuesday of the month|
|? (interrogation point)||Used in the day of the month and day of the week the fields||? in the day of the month field will read as no exact value|
Important! Note that all fields must be filled in when creating a cron job. If you don’t want to specify a field, fill it with the * (asterisk) symbol.
Only a few cron commands are used in the command line, making it easy to create, delete, and manage cron entries:
- crontab -e – used to edit system crontabs. This command will create a new crontab if it has not been created yet.
- crontab -l – Used to display crontab entries (cron jobs) and display the contents of the system crontab file.
- crontab -r – will delete the current crontab file.
- crontab -i – will prompt before deleting a user’s crontab. It is strongly recommended to use it with the -r flag, make the flag -ri.
How to use Crontab: examples of Crontab syntax
Start by using the crontab command to create your first crontab entry:
Then you will be redirected to the crontab file. To add new entries, simply choose a new line and continue with the cronjob.
Important! Keep in mind that crontab uses the current system time and date, which means the timezone is shared with the root system.
Schedule a task for a specific time
One of the easiest ways to test cron is to schedule a task at a specific time. For example, the following cron job will run our script.sh at the indicated time, which is August 15th, at 3:30 p.m..
30 08 10 06 * cat /helloworld.sh
View Crontab entries
Since all cron jobs are stored in a crontab file, you can view which ones are already running. To do this, use the following command to display the contents of your crontab files:
Edit Crontab entries
In order to modify the already existing crontab entries, use this command:
Schedule a task for every minute
It’s easy to schedule tasks to run every minute. In the following example, we will ask the cat running the command to run periodically:
Schedule a background task every day
To schedule a background task to run every day, you can use the @Daily cron command:
@daily cat /home/helloworld.sh
Keep in mind that the script will be executed at 12 p.m. every day.
Schedule a task for a certain time range
It is possible to schedule a task for a specific time range. For example, every day of the week, including weekends, from 8 a.m. at 5 p.m.. The end result would look like this:
00 08-17 * * * cat /home/helloworld.sh
Here is another example of the same cron, but just on weekends:
00 08-17 * * 6-0 cat /home/helloworld.sh
Schedule a cron job at the beginning of each month
In order to schedule a task at the beginning of each month, you can use the @monthly operator:
@monthly cat /home/helloworld.sh
Keep in mind that this will run the job at 12 p.m. the first day of each month. Likewise, there is a @annual operator who will perform the work on the first day of each year.
Schedule a task multiple times
Users can schedule a cron job to run multiple times, for example five times a day. In the following example, we will configure a task to run at 12 p.m., 3 p.m., 5 p.m., 7 p.m. and, 9 p.m..
0 12,15,17,19,21 * * * cat /home/helloworld.sh
Run a Linux command after every reboot
Similar to @Daily, @monthlyand @annual operators, bash also comes with a @to restart ordered. Users can use it to schedule a task that will run every time the system reboots or restarts:
@reboot cat /home/helloworld.sh
Where is the Crontab file located?
Depending on the system operating system, crontab will be located in three different locations:
- macOS – /var/at/tabs
- Debian-based systems – /var/spool/cron/crontabs/
- Red Hat based systems – /var/spool/cron
Additional Crontab Actions
Besides the current functionality, crontab is also capable of additional actions such as creating a cron execution log or disabling email notifications. See the sections below for more information.
Create a log file
The easiest way to log all output from cron jobs is to use the following logic:
* * * * * cat /home/helloworld.sh >> /var/log/logs.log 2>&1
This command will log all output from cron jobs to a file named logs.log
Since cron sends an email to the user after each job, disabling this feature can be beneficial to prevent spam. Just add this line at the end of your cron job:
Crontab allows its users to set environment variables. This can be done with the above crontab -e ordered. When defining variables, refer to the list below:
- PATH – default path for crontab.
- SHELL – shell by default.
- LOGNAME – owner of crontab. The information is taken from the /etc/passwd phone book.
- RESIDENCE – user’s home directory. The information is taken from the /etc/passwd phone book.
Cron jobs are one of the best ways to perform scheduled tasks Virtual servers and physical Linux systems. With cron jobs, users can schedule various tasks for their system. For example, perform system maintenance on a particular day or even schedule it to run every day of the week.
In this tutorial, we have covered the majority of crontab operations. We have also provided some practical examples that you can use when creating a crontab.
If you have any questions or ideas, feel free to leave a comment in the comment section.