Cron settings: features, step-by-step instructions and reviews

Very often, on Linux-like systems, some routine processes may need to be automated. For this, special planners are used. One of the most famous of them is Cron, the settings of which will be discussed in this article.

What is a cron?

The name of the program is derived from the Greek "chronos", meaning time. Which, in fact, is logical. The task of the demon is to execute commands recorded in it at certain moments. The implementation process itself is based on checking the crown tables and comparing them with the current date and time.

Crontab file

The most important element in configuring Cron is the crontab configuration file. It contains execution commands and paths to scripts. All of them are run as root. Since they are located in the /etc/cron.d directory, these files should be configured there.

cron settings

Each individual user of the system uses its own crontab file.

Different Linux distributions use their directories to store user preferences. For RedHat, this is var / spool / cron. On Debian and Ubuntu, this will be var / spool / cron / crontabs. And var / spool / cron / tabs in SUSE.

List of commands for managing crontab

Cron has a set of special directives with which you can control the scheduler. They must be used in conjunction with crontab. Here is a short list:

  • -u username. Sets the user with tasks and settings for which further actions will be performed. If you omit this key, the default user will be set.
  • -l. Displays the current list of tasks.
  • -e. Launches the editor for the task scheduler.
  • -r. Removes all available tasks from the list.

Task format

As mentioned above, the configuration of Cron consists in defining commands in a special file. It looks like a simple six-column entry:

  • the first indicates the number of minutes. The available interval is from 0 to 59. You can use several values, a range and special characters;
  • the second column is the clock. Values ​​from 0 to 23 can be used;
  • then the day. Here you can specify a number from 1 to 31;
  • the fourth is a month. The minimum value is 1, the maximum is 12;
  • the fifth is a week. 0 or 7 corresponds to Sunday;
  • the last is the executed command itself.

As mentioned, special characters can be specified in the file, β€œ*” or β€œ/”. For example, such an entry - 23 * / 2 *** echo β€œRun” means that every even hour and 23 minutes β€œRun” will be displayed.

cron setting

Features of the crontab file

When configuring Cron, you must remember that it has specific properties that should be considered when configuring:

  • each file must necessarily end with an empty line, otherwise the last item may be ignored, and sometimes the whole file can be ignored;
  • files located in directories with a dot in the name will also be ignored by the scheduler;
  • in the settings files you can use the pound signs "#". They are used to mark comment lines. This is very convenient for describing planned tasks and teams.

Additional variables

When configuring Cron, you can use special values ​​and abbreviations in commands. For example, to indicate the day of the week, you can use the following list:

  • sun - sunday;
  • mon - monday;
  • tue - tuesday
  • wed - environment;
  • thu - thursday;
  • fri - friday
  • sat is saturday.

For months, there are also separate letter values ​​- jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec.

cron centos setup

There are also separate variables for configuring Cron that can replace entire lines of commands:

  • @reboot. The command will start every time the computer starts;
  • @yearly. This task will be launched once a year. The equivalent record in numerical form can be this: 0 0 1 1 *. Its synonym can also be written as @annually;
  • @monthly. As the name suggests, this variable launches the command once a month. Its numerical analogue is 0 0 1 * *;
  • @weekly. This variable will be launched every week;
  • @dayly. Once a day;
  • @midnight. The launch will be made at midnight;
  • @hourly. Each hour.

Configuring Cron on Centos 7

The installation and configuration process itself is not much different from other similar Linux distributions. Before configuring Cron on Centos, you must install the cronie package on your system. You can do this with the yum install cronie command. To create a file with instructions, you need to execute crontab -e in the terminal. All Cron system settings will be saved at / var / spool / cron / username.

centos 7 cron tuning

Some useful examples for creating frequently repeated tasks

You can configure Cron to cover almost all the needs of any user.

  • 00 09-17 * * 1 - 5 / path / to the team / krona. This command will automatically carry out the planned action every working day from 9 to 5 hours every first minute;
  • 00 9.17 * * * / path / to the team / krona. In this example, the command is executed twice a day. The first at 9, the second at 5 o’clock. The β€œ*” sign indicates that the command will be executed every day, month and year;
  • Also, do not forget about symbolic variables. For example, @monthly will perform a task every month at the first minute of the first hour of the first day. And @daily will be launched every day.

Cron Scheduler User Reviews

As a rule, for ordinary and inexperienced users, Cron is just a set of characters. But for system administrators and avid Linux users, this utility is an indispensable tool. Many note the flexibility and performance of a well-tuned system.

cron d setup

With Cron, you can automate routine processes that are too difficult to perform manually at specific intervals. Whether it is creating backups of the system or updating it.

However, there are small nuances. For example, some sysadmins note that it would be nice to have in the scheduler the ability to implement commands every second. But the developers of the program more than once made it clear that they would not implement this option.

Novice users are usually lost in a lot of settings and difficulties when starting the scheduler daemon. For them there are more understandable shells. For example, for Debian-based distributions, it might be gnome schedule. The KDE version is called kcron.

conclusions

In general, Cron is an excellent tool for automating many routine processes and facilitating work with the system. For those who do not like the command line and the intricacies of manually configuring configuration files, there are several varieties of Cron in the graphical shell that simplify the interaction.

Other operating systems also have their own scheduler versions, for example, in Windows, this is NnCron. Another analogue is Task Scheduler. It is also available in the Microsoft product line. And its console version is schtasks.exe.

Source: https://habr.com/ru/post/K12038/


All Articles