Very often in Linux-like systemsIt is necessary to automate some routine processes. Special planners are used for this. One of the most famous of them is Cron, which will be discussed in this article.
What is Cron?
The name of the program is derived from the Greek"Chronos," meaning time. Which, in fact, is logical. The task of the daemon is to execute the commands written into it at certain times. The actual implementation process is based on checking the crown tables and comparing them with the current date and time.
The most important element of the Cron configuration iscrontab configuration file. It contains the execution commands and paths to the scripts. All of them are run as root. Since they are located in the /etc/cron.d directory, these files should be configured there.
For 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. In 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 manage the scheduler. Apply them in conjunction with crontab. Here is a short list:
- -u username. Sets the user whose tasks and settings will take further action. If you omit this key, the default user will be specified.
- -l. Displays the current task list.
- -e. Runs the editor for the task scheduler.
- -r. Removes all available tasks from the list.
As mentioned above, the Cron configuration is to set the commands in a special file. It looks like a simple record consisting of six columns:
- the first indicates the number of minutes. The available range is from 0 to 59. You can use multiple values, range and special characters;
- the second column is the clock. Values from 0 to 23 can be used;
- further - day. Here you can specify a number from 1 to 31;
- the fourth is the month. The minimum value is 1, the maximum value is 12;
- the fifth week. 0 or 7 corresponds to Sunday;
- The last command is the command itself.
As mentioned, you can specify in the filespecial characters, "*" or "/". For example, such a record of 23 * / 2 *** echo "Execute" means that every even hour and 23 minutes will display "Run".
Features of the crontab file
When configuring Cron, remember that it has specific properties that should be considered when configuring:
- each file must necessarily end with an empty string, otherwise the last item can be ignored, and sometimes the entire file;
- files placed in directories that have a point in the name will also be ignored by the scheduler;
- In the configuration files, you can use the "#" grid symbols. They are used to mark lines with comments. This is very useful for describing scheduled tasks and commands.
When configuring Cron, you can use special values and abbreviations in commands. For example, to specify the day of the week, you can use the following list:
- sun - Sunday;
- mon - Monday;
- tue - Tuesday;
- wed - environment;
- thu - thursday;
- fri - Friday;
- sat - Saturday.
For months there are also separate literal values - jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec.
Also, to configure Cron, there are separate variables that can replace entire command lines:
- @reboot. The command will start each time you start the computer;
- @ yearly. This task will be launched once a year. Equivalent to it the record in numerical form can be such: 0 0 1 1 *. Its synonym can still be written as @annually;
- @monthly. As you can guess from the name, this variable runs the command once a month. Its numerical counterpart is 0 0 1 * *;
- @ weekly. This variable will be started every week;
- @dayly. Once a day;
- @midnight. The launch will be made at midnight;
- @hourly. Each hour.
Configuring Cron in Centos 7
The installation and configuration process itself is smalldiffers from other similar Linux distributions. Before you configure Cron in Centos, you must install the cronie package on the system. You can do this with the yum install cronie command. To create the instruction file, you need to run crontab -e in the terminal. All Cron system settings will be stored at / var / spool / cron / username.
Some useful examples for creating repetitive tasks
You can configure Cron in such a way as to cover almost all the needs of any user.
- 00 09-17 * * 1 - 5 / way / to the team / crown. This command will automatically execute the scheduled action every working day from 9 to 5 hours every first minute;
- 00 9.17 * * * / path / to the team / crown. In this example, the command is executed twice a day. The first in 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 the symbol variables. For example, @monthly will perform the task every month on the first minute of the first hour of the first day. And @daily will be launched every day.
User Reviews about Cron Scheduler
As a rule, for ordinary and unsophisticatedCron users is just a set of characters. But for system administrators and inveterate Linux users, this utility is an indispensable assistant. Many note the flexibility and performance of a well-tuned system.
Using Cron, you can automate routine processes, which are too difficult to perform manually at certain intervals. Whether it's creating a backup system or updating it.
However, there are some small nuances. For example, some sysadmins note that it would be nice to have the ability to implement commands every second in the scheduler. But the developers of the program have repeatedly made it clear that this option will not be implemented.
Beginners are usually lost in a largenumber of settings and difficulties when starting the scheduler daemon. For them there are more understandable shells. For example, for distributions based on Debian, it can become a gnome schedule. The KDE version is called kcron.
Overall, Cron is an excellent tool forautomation of many routine processes and facilitating work with the system. For those who dislike the command line and the subtleties 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 ownversion of the schedulers, for example, in a Windows environment this is NnCron. Another analog is the Task Scheduler. It is also available in the line of systems from Microsoft. And its console version is schtasks.exe.</ p>