Suppose we need to run a specific task every day, or maybe every Saturday night at 12:00 am? On Unix-like systems, it is possible to automate the launch of repetitive tasks using the cron task scheduler daemon. This article will cover the basics of working with it.

Everything described in this article is tested on Linux Debian 6.0 squeeze, but in most cases it will work on other Unix-like operating systems.

Basic commands

To manage the task scheduler, use the crontab command with the following keys:

U user - defines the user whose tasks will be viewed / edited, the absence of this parameter sets the current user;
-l - shows a list of current tasks;
-e - starts the task scheduler editor;
-r - removes all current tasks.

Thus, to assign a specific task, you need to run the crontab -e command and write a list of necessary tasks line by line based on the cron syntax.

Cron syntax

In general form, the problem of crowns is a line of the form:

* * * * * command

Each asterisk in the line corresponds to a specific value:

The 0 and 7 in the day of the week represent Sunday, because in some countries the day of the week starts with Sunday. Accordingly, 1 is Monday, 6 is Saturday. In addition to those listed above, the following basic characters are allowed in the crontab file:

# - comment (lines starting with this symbol are not executed);
, - enumeration of values ​​(1,2,3,4);
/ - every n times (* / n - every n, * / 5 - every 5, * / 2 - every 2);
- - range of values ​​(1-5 - from 1 to 5, 4-6 - from 4 to 6).

From the above, it follows that the following entries correspond to the following lines:

0 5 * * * - every day at 5:00;
* / 10 * * * * - every 10 minutes;
0 0 1 1 * - January 1 of each year;
0 9 * * 1,3,5 - Monday, Wednesday and Friday at 9 am;
0 0 1 * * - every 1st day of the month.

You can also write the following predefined values ​​in the crontab file:

@reboot - on boot operating system;
@yearly - every year at midnight on January 1st
@monthly - every month at midnight on the 1st;
@weekly - at midnight every Monday
@daily - daily at 0:00;
@hourly - at the beginning of every hour.

Sample crontab file

The best way to understand the syntax of a crontab file is to use the following example: crontab -e -u user

# Hi, I am the crontab file of user, and this is what my master taught me
# I say hello to him after my download
@reboot echo "Hello Boss!"

# Every weekday at 6:45 am I run a script for it,
# which turns on the alarm
45 6 * * 1-5 /home/user/beep.sh

# While the owner gets to work or gets enough sleep on a weekend,
# I send him the latest news (every day at 8 am)
0 8 * * * /home/user/newsmail.sh

# I call the owner home at the end of the day
0 18 * * 1-5 echo "Come home, Master" | mail -s "End of business day" user

# And I congratulate him
@yearly echo "Happy New Year"

System administrators, and ordinary users it is often necessary to automate various maintenance and Linux tasks using scripts. This is very convenient, you just run the script, and it does whatever is needed without your intervention. The next step in this path is to configure the automatic launch of the right script at the right time.

It is for these tasks that Linux uses the cron system service. This is a scheduler that allows you to execute the scripts you need once an hour, once a day, week or month, as well as at any time you specify or at any interval. The program is often used even by other services of the operating system. In this article, we'll take a look at how to set up cron and walk you through some of the most commonly used examples.

In fact, Cron is a service, like most other Linux services, it starts at system startup and runs in the background. Its main task is to execute the right processes at the right time. There are several configuration files from which it takes information about what and when to run. The service opens the file / etc / crontab, which contains all the necessary data. Often, in modern distributions, the run-parts utility is prescribed there, which runs the necessary scripts from the following folders:

  • /etc/cron.minutely- every minute;
  • /etc/cron.hourly- every hour;
  • /etc/cron.daily- everyday;
  • /etc/cron.weekly- every week;
  • /etc/cron.monthly- every month.

These folders should contain scripts that need to be executed at the specified interval. Scripts must be executable and must not contain periods in their names. This makes the scheduler very easy for new users. Also, the crontab file contains the launch of the anacron command, which works in the same way as cron, only it is intended for tasks that need to be performed once in a long period, for example, once a day, week, month, year.

It allows you to perform them even if the computer does not always work and shuts down from time to time. The date of the task execution is last recorded in the file / var / spool / anacron, and then, the next time it is started, anacron checks whether the right process was started at the right time, and if not, then it starts it. The cron service itself is more designed to complete tasks during the day or with a precisely scheduled time and date.

Cron setup

To set the time, date and interval when you need to execute the task, use the special syntax of the cron file and a special command. Of course, you can always edit the / etc / crontab file, but this is not recommended. Instead, there is a crontab command:

It is always advisable to execute it with the -e option, then yours will be used to edit the rules. text editor default. The command opens you a temporary file that already contains all the current cron rules and you can add new ones. After the cron command completes, the file will be processed and all the rules will be added to / var / spool / cron / crontabs / username, and the added processes will be launched from the user from whom you added them.

Therefore, you need to be careful here, and if you need to execute scripts from root, then crontab must be executed from root, and not from the user. This often causes problems.

Crontab syntax

As I said, time is set with a special syntax, let's look at the syntax for setting up one cron task:

minute hour day month day_ of week / path / to / executable / file

I must say that it is imperative to write the full path to the command, because for commands run as cron, the PATH environment variable will be different, and the service simply cannot find your command. This is the second most common cause of cron problems. Date and time are indicated using numbers or the "*" symbol. This symbol means that you need to perform it every time, if in the first field - then every minute, and so on. Now let's move on to examples.

Examples of cron configuration

First, you can see the cron tasks for the superuser, for this you can use the -l option:

You can remove all existing tasks with the -r command:

Let's assume that we need to run our script at / usr / local / bin / serve as superuser. Some kind of maintenance script. The simplest example is to run it every minute:

* * * * * / usr / local / bin / serve

0 * * * * / usr / local / bin / serve

We start at the zero minute of the zero hour, every day, this is at 12 at night:

0 0 * * * / usr / local / bin / serve

0 0 1 * * / usr / local / bin / serve

It is possible on any day, for example, on the 15th:

0 0 15 * * / usr / local / bin / serve

On the first day of the week of the first month of the year, 0 hours 0 minutes:

0 0 * 1 0 / usr / local / bin / serve

Or on the zero day of the week of every month:

0 0 * * 0 / usr / local / bin / serve

You can choose any minute, hour and day of the week, for example, 03:30 pm on Tuesday:

30 15 * * 2 / usr / local / bin / serve

Monday is considered the first day, Sunday is the seventh or zero day. You can also write the abbreviated name of the day of the week, for example sun - Sunday:

30 15 * * sun / usr / local / bin / serve

In order to indicate a certain interval, you need to use the "-" symbol, for example, every hour, from seven in the morning to seven in the evening:

0 7-19 * * * / usr / local / bin / serve

If you need to run the command multiple times, you can use the "," separator. For example, let's run the script at 5 and 35 minutes after five (16:05 and 16:35), every day:

5.35 16 * * * / usr / local / bin / serve

You may want to not specify the time separately, but simply specify the interval with which you want to run the script, for example, once every 10 minutes. To do this, use the slash separator - "/":

* / 10 * * * * / usr / local / bin / serve

In addition, for some commonly used sets, variables were invented, here they are:

  • @reboot- at boot, only once;
  • @yearly, @annually- once a year;
  • @monthly- once a month;
  • @weekly- once a week;
  • @daily, @midnight- everyday;
  • @hourly- every hour.

For example, the command to run the script once an hour will look like this:

@hourly / usr / local / bin / serve

If you are going to add a script to one of the folders, then, as I already said, you need its name to be without dots and it has permission to execute:

sudo vi /etc/corn.daily/basckup

The script should look like this. Now you know how to set up cron, it remains to check how everything works.

Debugging work

After you have configured the rules, I would also like to check if they work. To do this, we wait for the time when the script should already be executed and look at the cron log. Sometimes it is located in / var / log / cron, and sometimes it is written to syslog. For example, I have a line like this in my crontab:

It should be executed at 19.40 every day, now we are looking at the log:

grep CRON / var / log / syslog

And we see that in our log it really is and is being executed completely successfully. If there were any errors, then a message would be displayed immediately.

If you need to check a script that is located in one of the specialized folders, then it's even easier, just run run-paths, passing it the required folder or even the script itself in the parameter:

sudo run-paths /etc/cron.daily/

conclusions

In this article, we looked at how to set up cron for easy scheduling of automated tasks. Hope this information was helpful to you.

Very often in Linux-like systems, it may be necessary to automate some routine processes. 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 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 the commands written into it at certain moments. The implementation process itself is based on checking the crown tables and comparing them with and time.

Crontab file

The most important element of cron configuration is the crontab configuration file. It contains execution commands and paths to scripts. They all run as superuser. Since they are located in the /etc/cron.d directory, these files must be configured there.

A separate crontab file is used for each individual user on the system.

Different Linux distributions use their own 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 that can be used to control the scheduler. They must be used in conjunction with crontab. Here's a small list:

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

Task format

As mentioned above, setting up Cron consists in setting commands in a special file. It looks like a simple six-column entry:

  • the first indicates the number of minutes. The available range is from 0 to 59. Multiple values, range and special characters can be used;
  • the second column is hours. Values ​​from 0 to 23 can be used;
  • further - the day. Here you can enter a number from 1 to 31;
  • the fourth is a month. The minimum value is 1, the maximum is 12;
  • fifth - week. 0 or 7 corresponds to Sunday;
  • the last command is the executable itself.

As mentioned, you can specify special characters in the file, "*" or "/". For example, such an entry - 23 * / 2 *** echo "Run" means that every even hour and 23 minutes the inscription "Run" will be displayed.

Features of the crontab file

When setting up Cron, remember that it has specific properties that should be taken into account when configuring:

  • each file must end with an empty line, otherwise the last point can be ignored, and sometimes the entire file;
  • files located in directories with a dot in the name will also be ignored by the scheduler;
  • hash symbols "#" can be used in configuration files. They are used to mark commented lines. This is very useful for describing scheduled tasks and commands.

Additional variables

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

  • sun - Sunday;
  • mon - Monday;
  • tue - Tuesday;
  • wed - Wednesday;
  • thu - Thursday;
  • fri - Friday;
  • sat - Saturday.

There are also separate literal values ​​for months - jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec.

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

  • @reboot. The command will start every time the computer is started;
  • @yearly. This task will run once a year. The equivalent numeric notation can be as follows: 0 0 1 1 *. Its synonym can also be written as @annually;
  • @monthly. As you might guess from the name, this variable launches the command once a month. Its numeric analogue is 0 0 1 * *;
  • @weekly. This variable will run every week;
  • @dayly. Once a day;
  • @midnight. The launch will take place at midnight;
  • @hourly. Every hour.

Setting up Cron in Centos 7

The installation and configuration process itself is not much different from other similar ones. Linux distributions... Before setting up Cron on Centos, you need to install the cronie package on the system. This can be done using the yum install cronie command. To create a file with instructions, you need to run in the terminal crontab -e. Everything system settings Cron will be saved to / var / spool / cron / username.

Some useful examples for creating frequently recurring tasks

You can customize Cron in such a way as to cover almost all the needs of any user.

  • 00 09-17 * * 1 - 5 / path / to the command / crown. This command will automatically execute the scheduled action every working day from 9 am to 5 pm every first minute;
  • 00 9.17 * * * / path / to command / crown. 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, don't forget about symbolic variables. For example, @monthly will run the task every month in the first minute of the first hour of the first day. @Daily will run every day.

Cron (crowns), Crontab (crontab), Task Manager- all those associated with "site building" have heard these names many times. So what is Cron? How to work with him? Why do you need Cron and how to set it up correctly? We will analyze all these questions today.

The first thing I want to note right away: Cron, Crontab, Task Manager- it's all the same, do not be embarrassed by the variety of names.

In the practice of any web-master, there will invariably arise the need to launch any tasks on a schedule. Those. You just prescribe the command you need to perform a task at a given time, and that's it. Then everything happens without your participation - automatically, and what is most pleasant at the time you need.

For example, you need to periodically send notifications to partners on e-mail... Or you want to set up automatic greetings or reminders. Or you have a directory of sites and you want the directory script to check backlinks, etc. at a given frequency. All these tasks can be easily automated by entrusting it to Cron or, as they say - Task Scheduler... And there are scripts in which Cron is simply necessary for normal operation.

Almost all modern Hostings provide a ready-made function Task Scheduler... If there is no such function on the Hosting, then it is better not to contact such a Hosting company. Cron is no longer a luxury these days, and if the Hosting does not offer a task scheduler in the package of services, you better look for another company, for sure there will be no other important functions.

So, let's look at all the practical steps for setting up Cron using the example of the Russian cPane l (site control panel) Hudson Hosting Company... Why exactly Hudson? It's simple - my site lives here and I really like this Hosting: a full package of all the most possible services for a very reasonable price.

Although setting Task Scheduler happens about the same everywhere.

We go to the site control panel, in this case it is cPanel (there are others). Access is provided to you immediately after purchase Hosting, go down below and find such a section, Additional tools:

We find in this section Task Manager and click on it. This menu is sometimes called the Cron Panel. Here's how Cron is characterized:

"The task scheduler allows you to run commands at a specified time without your intervention. This allows you to automate regularly repetitive tasks. The task scheduler is very flexible and allows you to automate the execution of any commands you want to run. For example, you can tell the scheduler to delete temporary files. every week so they don't take up too much disk space. "

You can choose from two options for controlling the Cron:

Standard and Advanced (Unix style), consider each of them.

First option. Click on the Standard button, the task setup window opens:

As you can see, everything is very simple here. You just need to set the execution time of the command, specifying for this in the appropriate fields when the command will be run, at what time: Minutes, Hour, Day, Month, Day of week... And of course, write the command itself to run in the field: Command to run.

The values ​​can be written either manually (in the left field) or use the drop-down list (arrows on the right).

Let's see what we have written here. And we wrote the following task: run the command for execution:
/ usr / bin / php /home/freeman/domains/public_html/cron/new_day.php
every 30 minutes, every 6 hours, every day of January, if it falls on a Monday.

After setting the job, click the Add New Cron Job button.

It's not clear what you wrote? :) Let's consider the second option, and then explain in more detail. We return to the previous page.

Second option. Click on the button Advanced (Unix style), the task setup window opens:

This option for setting the task to Kron, I like more. And although it is called Advanced, in my opinion it is simpler and more convenient.

Here, as in the first case, you need to indicate when the command will be run, we also indicate: Minutes NS, Hour NS, Day, Month, Day of week and the task itself for execution in the field Command... After that, click the Add Cronjob button. The task is set. The page is being updated:

And you can add a new task by repeating the whole procedure again. You can delete the task by clicking on the cross opposite the task on the right.

There is one more optional parameter, but I recommend prescribing it: this email address... A report on the execution of the command will be sent to this address. The field is optional, but it is better to enter the address to be aware of the matter, if something goes wrong, Cron will inform you about it in a letter, and if the letter is empty, then everything is OK!

What have we written here? Run the command at 11 minutes, every 2nd hour, every day, every month, 1st, 3rd, 5th, 7th days of the week.

In the selection box: Minutes specify the minute (or minutes) in which the task will run, in the field Hour specify the hour (or hours) in which the task will be executed, as well as the day (s) and month (s), in each field you can specify both a specific time and an interval.

For example, you can specify not just 11 minutes, but 11-15 (interval), which means that the task will run every 11, 12, 13, 14, 15 minutes. Or specify specific minutes, for example: 11, 14, 18 - this means that the task will run at 11, 14, and 18 minutes. You can also use the sign * (asterisk)- denotes each. If you put * (asterisk) in the Minutes field, the task will be launched every minute, i.e. 60 times an hour.

In each field, you can specify both a specific time and an interval, as well as use asterisks.

Recording */2 means: every 2 hours... All of these record variations apply to all fields ( Minutes NS, Hour NS, Day, Month, Day of week) when specifying a task to Cron. You understand, there can be millions of options, Run the task, at least every minute all year round. Of course, such a need almost never arises, but theoretically there is such a possibility.

And also keep in mind some Hostings impose restrictions on the launch of Krona, for example, no more than 3 - 10 times per hour. Why? Quite a significant load on the server is created. For violation, your account may be blocked.

Now let's figure out how the field is filled Command to run(in the first option) or Command (in the second option).

The task is set as follows.

First of all, you specify the path to PHP on your server, on my server the path / usr / bin / php may be different for you, check with your server admins, after the path to PHP put a space and write the full internal path to the file that should run Cron... Suppose I need Cron to launch the mail.php file, knowing the internal path to the file, I write the following:

public_html / cron / mail.php

those. in the public_html root folder, there is a cron folder in which the mail.php file is located, and the entire command to run the mail.php file will look like this:

/ usr / bin / php /public_html/cron/mail.php

On different hosting, the path to PHP may differ, and it may be written in different ways, the root folder of the site location also does not always exist public_html, therefore, if there is no specific example of recording a task for Kronu, then it is better to ask the administrator about this, describing what you want to get in the end. Good

(Scheduled tasks) is used to configure the execution of commands on a schedule or at a strictly appointed time. By using CronTab You can set a specific time, and exactly at this time the command you specified will be executed. Configure CronTab it is possible in such a way that the task will be executed once every month, day, hour and every minute or for example every 10 minutes... You can also combine, for example, you need to complete the task once an hour for the first 2 hours, then do not perform the next 2 hours.

Also one of the convenience is the ability to send the results of the tasks to email. To do this, click on the appropriate inscription and enter your email.

Quest Wizard

First acquaintance with Cron for us starts with the opening Quest Masters... With the help of the wizard, you can configure the execution time of scripts without any serious knowledge of administration, simply by choosing a certain combination from the drop-down lists. You also need to enter the path to the script or the command that will be executed in Cron.

Let's see an example of running a script written in the language PERL to run every 5 minutes.

After clicking on the button, under the main window Cron a new entry will appear with your first configuration. The number of such records is not limited.

We now have the first task that we can turn on/switch off, change or delete using the buttons on the right, opposite each of the tasks:

Enable / disable the task;
- change the task;
- delete the task.

Manual configuration of CronTab.

Also in the section there is a manual setting that allows you to adjust the script launch time in the usual way, for those who are familiar with Cron.

Time, interval

Minutes can be from 0 before 59
The clock can be from 0 before 23
The day of the month can be from 1 before 31
Month maybe 1 before 12
The day of the week can be from 0 before 7 where 0 and 7 are Sunday

Configurable CronTab to complete tasks not only in a certain
time, but also every minute, hourly, daily, weekly or monthly,
using the combination * / x

Examples of

* / 5 * * * * - run command every five minutes
0 * / 3 * * * - run every three hours
0 12-16 * * * - run the command every hour from 12 to 16 (at 12, 13, 14, 15 and 16)
0 12,16,18 * * * - run the command every hour at 12, 16 and 18 hours

*/1 * * * * / usr / bin / php ~ / site.ru / public_html / test.php- run every minute of the php script test.php
0 */1 * * * / usr / bin / perl ~ / site.ru / public_html / test.pl- run every hour perl script test.pl

Command

It is necessary to set the path to the script from the home directory
For example: public_html / cgi-bin / script.pl
The system will insert the symbol itself ~/ (this combination replaces the full path)
It will turn out: ~ / public_html / cgi-bin / script.pl
If you put the character at the end of the path & (ampersand), the script will run in the background.
Setting this symbol is optional.