How to add a Cron Job
One of the most well-known and convenient utilities for automating tasks on a server is Cron. Executing commands and scripts on a schedule is what this program does. These are basic actions, but they enable us to automate tasks such as backup, updating and data collection, cleaning temporary files, and much more.In this article we will focus on the crontab file, its syntax, and how to write commands using specific examples.
Cron and Crontab
Why are we talking about adding tasks to cron, but actually using the crontab command to do so?Cron - this is a system daemon, service, or utility. That is, the program itself monitors the specified commands and time and implements the functionality.
Crontab - this is essentially a file where we record the time, the task to be performed, and some other parameters.
Adding a task and the structure of the crontab file
To open the editor for editing or creating a cron job for the current user use the command:crontab -e
The default editor will open (e.g., nano or vim). You can find out how to change it in the article How to change the editor for crontabBe careful, in the classic qwerty keyboard layout, the letter E is located next to R. If you enter the crontab command with the -r parameter this will delete all rules from the current user's crontab file.

At the top of the file you can see the environment variables. They set the conditions for executing tasks in Cron. The example uses two variables that are commonly found on production servers.
The basic variables in crontab
- MAILTO - sets the email address to which the cron job completion report will be sent. MAILTO="" disables sending;
- PATH - list of directories with executable files;
- SHELL - sets the command interpreter for running tasks. By default: sh;
- HOME - the directory from which cron runs commands. An important variable if relative paths are used in commands (e.g. ./cron.sh), which is bad practice.
The schedule is set by five values:
- Minutes of the hour (0-59)
- Hours of the day (0-23)
- Day of the month (1-31)
- Month of the year (1-12)
- Day of the week (0-7 (0 and 7 — Sunday))
- * - corresponds to any value (every minute, every hour, etc.);
- , - value list separator;
- - - range of values;
- / - "divisor" of a value.
* * * * * cron.sh >> /root/cron.log 2>&1
It can be interpreted as: Execute the specified command every minutes(*) every hours(*) every day of the month(*) every day of the year(*) every day of week(*). To put it simply - every minutes. This is the minimum frequency with which cron can execute a task.Please note that the path to the executable file cron.sh is not specified, but the task is still executed successfully:

This happens because the PATH environment variable is set to the directory where this script is located. However, this is bad practice and such entry was used only as an example of how environment variables work. It is always better to use the full path to the executable file or script on production servers.
Let's change the task schedule and add a specific start time, for example:
0 * * * * cron.sh >> /root/cron.log 2>&1
This task will run at 0 minute of every hour, i.e. once per hour.As you may have understood 0 indicates a specific minute, zero. To ensure that the command runs every hour at 20 minutes the task must be modified as follows:
20 * * * * cron.sh >> /root/cron.log 2>&1
The same logic applies to other time and date values. Let's consider the second task in the example:
20 4 * * * /usr/local/bin/backup.sh >> /var/log/backup.log 2>&1
It runs every day(*) at 4:20 a.m.Let's assume we don't need to create a backup so frequently, so let's change this task to run on Sundays (0 or 7) at 3 a.m.:

It is also possible to specify several specific values separated by commas:
0 3 * * 0,3 /usr/local/bin/backup.sh >> /var/log/backup.log 2>&1
This task will run every Sunday and Wednesday at exactly 3 a.m.To specify the period or range of values use -. The task will be run on weekdays, Monday through Friday (1-5), at 3 a.m.:
0 3 * * 1-5 /usr/local/bin/backup.sh >> /var/log/backup.log 2>&1
Очень часто для запуска задания через каждый установленный промежуток времени используется выражение вида:
*/30 * * * * /usr/local/bin/backup.sh >> /var/log/backup.log 2>&1
This Cron job will run every 30 minutes.However, we don't need that many backups, so it makes more sense to run a backup every two days:
0 0 */2 * * /usr/local/bin/backup.sh >> /var/log/backup.log 2>&1
In addition to time fields the schedule can be defined using special human-readable abbreviations.
Time-based crontab reductions
- @reboot - run a task after the system boots up;
- @hourly - every hour;
- @daily - once a day (at 00:00);
- @midnight = @daily;
- @weekly - once a week (Sunday at midnight);
- @monthly - once a month (on the 1st day at 00:00);
- @annually - once a year (January 1 at 00:00);
- @yearly = @annually.
To save changes to Crontab and exit, if you are using the Nano editor, press Ctrl + O and Ctrl + X.
If you are using Vim, press Esc, enter the command :wq, and press Enter.
You will receive a notification when new tasks are added:

To view a list of cron jobs for the current user use the command:
crontab -l

We have thoroughly reviewed the process of adding cron jobs and the Crontab syntax. But if you have any difficulties with this task on our VPS and dedicated servers, you can always contact technical support, we will be happy to help you!
29 May 2025, 22:11:27