Customize task scheduler - Crontab. Simple Cron Setup on Linux CRON Time

System administrators, and ordinary users often have to automate various maintenance tasks and work with Linux using scripts. It is very convenient, you just run the script, and it does everything you need without your intervention. The next step in this way is to configure automatically launching the desired script at the right time.

It is for these tasks in Linux using a CRON system service. This is a scheduler who allows you to perform the scripts you need once an hour, once a day, a week or month, as well as at any time you specify or through any interval. The program is often used by other operating system services. In this article, we will look at how the CRON is configured and we will analyze the main frequently used examples.

In fact, CRON is a service, like most other Linux services, it starts at the start of the system and works in the background. Its main task to perform the necessary processes at the right time. There are several configuration files from which it takes information about what and when to perform. The service opens the / etc / crontab file, which indicates all the necessary data. Often, in modern distributions there is a launch of the RUN-Parts utility, which starts the desired scripts from the following folders:

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

These folders must be scripts that need to be performed with the specified interval. Scripts must have the rights to execute and their name should not contain points. It greatly facilitates the work with the scheduler for new users. Also in the Crontab file, the Anacron command is registered, which works as well as CRON, only intended for the tasks to perform once in a long period, for example, once a day, week, month, year.

It allows them to perform them even if the computer does not always work and turns off from time to time. The setting date of the task is the last time written to the / var / spool / anacron file, and then, the next time you start Anacron checks whether the desired process was started at the right time, and if not, then it starts it. The CRON service itself is more designed to perform tasks during the day or with precisely painted time and date.

Configuring Cron

To adjust the time, dates and intervals when you need to perform the task, the Special CRON file syntax and a special command are used. 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 option -, then your text editor will be used to edit the rules. The command opens the temporary file to you, in which all current CRON rules are already presented and you can add new ones. After completing the CRON file, the file will be processed and all the rules will be added to / VAR / SPOOL / CRON / CRONABS / username, and added processes will be started from that user from which you added them.

Therefore, it is necessary to be neat here, and if you need to execute scripts from the root, then the crontab must be made from the root, and not from the user. It often becomes cause.

Syntax Crontab.

As I said, time is specified by special syntax, let's look at the syntax of the configuration of the same CRON task:

minute hour day month day_Nedeli / path / to / executable / file

It must be said that it is necessary to write a full path to the command, because for the commands running on behalf of CRON, the path variable will differ, and the service will simply not be able to find your team. This is the second most common cause of problems with CRON. Date and time are specified using the digit or symbol "*". This symbol means that you need to perform every time if in the first field - then every minute and so on. Well, now we turn to the examples.

Cron settings examples

You can first see the CRON tasks for the superuser, for this you can use the -L option:

You can delete all existing command tasks -r:

Let us assume that we need to run on behalf of the superuser, our script at / usr / local / bin / serve. Some serving script. The easiest example is to run it every minute:

* * * * / USR / Local / Bin / Serve

0 * * * * / USR / Local / Bin / Serve

Run in the zero minute of zero hour, every day, it is at 12 nights:

0 * * * / USR / Local / Bin / Serve

0 0 1 * * / USR / Local / Bin / Serve

You can any day, for example, the 15th:

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 in the zero day of the week of each month:

0 0 * * 0 / USR / Local / Bin / Serve

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

30 15 * * 2 / USR / Local / Bin / Serve

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

30 15 * * Sun / USR / Local / Bin / Serve

In order to specify 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 want to start the command several times, you can use the separator ",". For example, launch a script at 5 and 35 minutes later (16:05 and 16:35), every day:

5,35 16 * * * / USR / Local / Bin / Serve

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

* / 10 * * * * / USR / Local / Bin / Serve

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

  • @reboot. - when loading, only once;
  • @yearly, @annually - once a year;
  • @Monthly. - once a month;
  • @weekly - once a week;
  • @Daily, @midnight - everyday;
  • @hourly. - each hour.

For example, it will just look like a script start command once per hour:

@hourly / USR / Local / Bin / Serve

If you are gathered to add a script into one of the folders, then I have already said, it is necessary for his name to be without points and he had rights to execute:

sudo vi /etc/corn.daily/Bascuup.

The script should look like this way. Now you know how to configure CRON, it remains to check how everything works.

Debugging

After you configured the rules, I would also like to check whether they work. To do this, we are waiting for the time when the script must already be completed and watch the Cron log. Sometimes it is in / Var / Log / Cron, and sometimes writes to syslog. For example, I have such a string in CRONEB:

It must be performed at 19.40 every day, now we look at the log:

gREP CRON / VAR / LOG / SYSLOG

And we see that in our log it really is and is performed entirely successfully. If there were any errors, the message would immediately be displayed.

If you need to check the script that is in one of the specialized folders, then it's even easier, just run the run-paths by passing it to the option you want or even the script itself:

sudo Run-Paths /etc/cron.daily/

conclusions

In this article, we considered how to configure CRON for convenient automatic tasks. I hope this information was useful for you.

Suppose we need to run a certain task every day, or maybe every Saturday in 12 nights? In UNIX-like systems, it is possible to automate the launch of repetitive tasks using the CRON task scheduler. This article will consider the basics of working with it.

All described in this article is tested in Linux Debian 6.0 Squeeze OS, however, in most cases will work on other UNIX-like operating systems.

Main teams

To manage the task scheduler, the CRONTAB command is used 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 - launches task scheduler editor;
-R - removes all current tasks.

Thus, to assign a specific task, you must execute the CRONTAB -E command and build a list of the necessary tasks based on the CRON syntax.

Syntax Cron.

In the general form, the crown task is a string of the form:

* * * * * team

Each sprocket in the string corresponds to a certain value:

0 and 7 at the bottom of the week denotes Sunday, because in some countries the day of the week begins on Sunday. Accordingly, 1 - Monday, 6 - Saturday. In addition to the above, the following main characters are allowed in the Crontab file:

# - Comment (rows starting with this symbol are not performed);
- enumeration of values \u200b\u200b(1,2,3,4);
/ - every n times (* / n - every n, * / 5 - every 5, * / 2 - every 2);
- - interval of values \u200b\u200b(1-5 - from 1 to 5, 4-6 - from 4 to 6).

It follows from the above 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 1 * * - every 1st month.

Also in the Crontab file, you can record the following predefined values:

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

Example CRONTAB file

It is best to understand the syntax of the Crontab file can be used on the following example: crontab -e -u user

# Hello, I am the user CRONTAB user, and this is what my master taught me
# I greet him after my download
@reboot echo "Hi, owner!"

# Every working day at 6:45 I run a script for him,
# which includes alarm clock
45 6 * * 1-5 /home/user/Beep.sh

# While the owner gets to work or falls out on the day off,
# I send fresh news for him (every day at 8 am)
0 8 * * * /home/user/newsmail.sh

# I call the owner home at the end of the working day
0 18 * * 1-5 Echo "Come home, owner" | Mail -S "End of the working day" User

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

CRON (CRON), CRONTAB (KRONTAB), Task Manager - These names are all who are associated with the "site building" heard repeatedly. So what is CRON? How to work with him? Why do you need CRON and how to configure it correctly? We will analyze all these questions today.

The first thing I want to immediately note: Cron, Crontab, Task Manager - This is all the same thing, do not confuse the variety of titles.

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

For example, you need to periodically send notifications to partners by email. Or you want to install automatic greetings or reminders. Or you have a directory of sites and you want the catalog script to check the backlinks, etc. with the specified periodicity. All these tasks can be easily automated by charging this Cron or, as they say - Task Scheduler. And there are scripts in which the crowns are simply necessary for normal operation.

Almost all modern hosting hostesses provide a ready-made function. Task scheduler. If there is no such function on the hosting, it is better not to contact such a hosting company. Cron Nowadays, this is no longer a luxury and if the hosting does not offer the task scheduler in the package, it is better to look for another company, for sure there will be no other important functions.

So, let's consider all practical actions on the CRON tincture on the example of a rusted CPANE L (site management panel) Huson hosting company . Why exactly Hudson ? Everything is simple - my site lives here and I really like this hosting: full package of all the most possible services for a very reasonable price.

Although setting Task scheduler It happens about everywhere equally.

We go to the Site Management Panel, in this case it is CPanel. Access to you immediately after shopping Hosting , go down the lower and find this section, Additional tools:

Find in this section Task Manager And click on it. Sometimes this menu is called the CRON panel. This is how Cron is characterized:

"The task scheduler allows you to run commands at the specified time without your participation. This allows you to automate regularly repeated operations. The task scheduler is configured very flexible and allow you to automate the execution of any commands that you want to run. For example, you can specify the scheduler to delete temporary files Every week so that they do not occupy too much space on your disk. "

The choice provides two options for controlling the crown:

Standard I. Advanced (in the style of UNIX), Consider each of them.

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

As you can see everything is very simple. You just need to set the command execution time by specifying for this in the appropriate fields when the command is started, how much: Minutes, Hour, Day, Month, Day of the week. And of course to register the command to start in the field: Team for running.

The values \u200b\u200bcan be written both in the manual (in the left field), and use the drop-down list (the arrows are deliberate).

Let's deal with what we wrote here. And we wrote the following task: Run the command to execute:
/ usr / bin / php /home/preman/domains/public_html/cron/new_day.php
Every 30 minutes, every 6 hours, every day of January, if he hits Monday.

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

It is not clear what they wrote? :) Let's consider the second option, and then explain more. Return to the previous page.

Second option. Click on the button Advanced (in the style of UNIX)The task installation window opens:

This option to set the task is the krona, I like more. And even though it is called an advanced, in my opinion easier and more convenient.

Here, as in the first case, you need to specify when the command will be launched, just specify: Minutess, Hours, Day, Month, Day of the week and the assignment itself in the field Team. After that, click the Add Cronjob button. The task is set. The page is updated:

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

There is another not required parameter, but I recommend it to prescribe: it email address. This address will receive a report on the execution of the team. The field is optional, but better to enter the address to be aware of the case, if something goes wrong, the crown will inform you about it in the letter, and if the letter came empty it means everything is OK!

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

In the selection field: Minutes Specify a minute (or minutes) to which the task will be launched in the field Hour Specify an hour (or clock) in which the task will be performed, as well as day (s), and month (s), in each field you can specify as a specific time and the interval.

For example, you can specify not just 11 minutes, but 11-15 (interval), it means that the task will be launched every 11, 12, 13, 14, 15 minutes. Or indicate Cocktails, for example: 11, 14, 18 - this means that the task will be launched at 11, 14, and 18 minutes. You can still use a sign * (asterisk) - Indicates each. If you put * (star) In the minute field, then appropriate task will be run every minute, i.e. 60 times per hour.

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

Record */2 indicates: Every 2 hours. All these variants of records are applicable to all fields ( Minutess, Hours, Day, Month, Day of the week) When specifying a task to the krona. You yourself understand the options can be millions, run the task for execution, at least every minute all year round. Of course, such a need almost never occurs, but theoretically, such an opportunity is.

And yet have a form of some hosting lists restrictions on the launch of Krone, for example, no more than 3 - 10 times per hour. Why? A rather tangible load on the server is created. For violation, your account can block.

Now let's figure out how the field is filled Team for running (In the first variant) or the command (in the second version).

The task is set as follows.

First of all, you specify the path to PHP on your server, I have a path on the server such / usr / bin / php you can have others, check with the admins of your server, after the path to PHP put the gap and write the full internal path to the file that should run cron. Let me say that I need the crown to start the mail.php file, knowing the internal path to the file I write the following:

public_HTML / CRON / Mail.php

those. In the root folder Public_html, there is a CRON folder in which the mail.php file is lying and fully the command on the launch of the mail.php file will be removed like this:

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

On different hosting, the path to PHP may differ, and recorded in different ways, the root folder of the site location also does not always happen public_html, Therefore, if there is no specific example of writing the crown job, then it is better to ask the administrator about it, describing what you want to get in the end. Good

In our materials dedicated to Ubuntu Server, there is a question of performing any tasks on schedule from time to time. In order not to explain the same thing every time, we decided to create this material, which should help system administrators to master and effectively use the task scheduler in Linux.

Ubuntu Server is used as a task scheduler cron. - Planner with command line interface. It is an important part of the system and begins to function immediately after installation, performing various system tasks. Our goal is to deliver it to the service, especially since it is not so difficult as it seems.

There are two types of schedules. cron.: Custom and systemic. They differ in the fact that the first is created by users and is executed taking into account custom rights, the second is used in administrative or systemic purposes and can be run on behalf of any user.

To create or change the user schedule, type:

Crontab -e.

When you first start the utility will offer to choose the editor, we recommend choosing mCedit. (requires the installed MC), or another editor with which you can work.

The format of the schedule lines has the form:

Minute hour day month day_Nedi team

  • Minute- time in minutes from 0 to 59
  • Hour- from 0 to 23
  • Day- day of month from 1 to 31
  • Month- from 1 to 12 or alphabetic designations Jan - Dec
  • Day of the week - from 0 to 6 (0 - Sunday) or SAT - Sun
  • Team - A string in the format of the command interpreter which will be executed, the type is allowed team1 && Team2 To start multiple teams in a row.

The values \u200b\u200bof minutes, hours, days can be indicated as follows:

  • Value - the number indicating the date or time is allowed wildcard * allowing a full range of values
  • Multiple values - It is allowed to indicate several values \u200b\u200bthrough the comma, for example, 2,14,22
  • Range of values - indicated through a hyphen, for example 2-10
  • Step values - indicated through the fraction, the denominator is set, for example * / 3 - each third value of 0, 3, 6, 9, etc. As a numerator there must be a range of values \u200b\u200bor an asterisk.

Consider the following example of the record:

0 8-19 / 2 * * 1 / Home / Ivanov / Test

It means that every second hour from 8 to 19 (8, 10,12,14,16) on Mondays to launch the TEST script in Ivanov's home catalog.

Immediately we want to warn you from a common mistake, when specifying periodic execution, all dates must be specified explicitly, the asterisk denotes a full range of values, and not their absence. For example, if you need to execute a script every hour from 10 to 15 will incorrectly:

* 10-15 * * / Home / Ivanov / Test

This string will launch the script every minuteranging from 10 to 15 hours. It will be right:

0 10-15 * * * / Home / Ivanov / Test

This record will allow you to run the script at the beginning of each hour of the specified range.

In addition to the date, you can use a number of special lines:

  • @reboot.- execute the command when rebooting
  • @Yearly or @Annually - Perform January 1, similarly recording: " 0 0 1 1 * "
  • @Monthly. - Perform 1 numbers of each month, similar to " 0 0 1 * * "
  • @weekly. - Perform every Sunday, is equivalent. " 0 0 * * 0 "
  • @Daily.or @midnight - Daily at midnight, " 0 0 * * * "
  • @hourly - once a hour, " 0 * * * * "

So for the daily execution of our script every midnight can be written:

@Midnight / Home / Ivanov / Test

After completing the schedule compiling, save the file and exit from the editor. Custom schedule will be saved in / VAR / SPOOL / CRON / Crontabs under the name of the current user.

For system and administrative tasks provided a file / etc / crontabthe syntax of the records in it is distinguished by the presence of an additional value - the user, the task will be launched from whose name:

Minute hour day month day_Nedi user team

An example of such a record:

0 19 * * 1-5 root / etc / backup

According to which at 19:00 from Monday to Friday the script will be launched / etc / backup On behalf of the user root.

This file also contains system schedules, so it should be used to edit it with caution. All system and administrative tasks should be placed in it.

As you can see cron.it is fairly easy to use, but at the same time provides rich opportunities for setting up schedules in Ubuntu Server. We hope this article will help administrators to master this tool.

I heard a lot about the fact that in Linux there is a convenient task scheduler CRON. However, I did not have the need to use it, and I did not want to understand his settings ... The console, a lot of English bugs ... It was scary. But, since, my concerns were in vain - everything is easy to easily. The article will consider how to configure the execution of their scripts on the schedule, and as an example, install a "cuckoo".

For the beginning of a little about how it works at all.

When starting the system, the CRON demon starts. They can be managed (stop / run / recognize status) by command: Sudo Service CRON (STOP / START / STATUS). But it is rare when you need.

The Cron demon himself is asleep most of the time, and slightly opens the eye once a minute, to check for the assignment for this time. If there are no tasks, it goes again in a hibernation.

Jobs are in files names that are equal to user names, and the files themselves are in the / var / spool / cron / crontabs folder. The folder is protected from outside interference and is available only to the superuser. But, each user can customize the schedule for its tasks, not knowing the password from the root (superuser).

What would customize Cron For a regular user, it is enough to dial:

If you need to create a task for another user, the start is made by the command:

sudo crontab -u user -e

Watching the user We write the desired user, such as root.
When you first start, there will be a question about the editor ... I like Nano. It is simple and runs without a graphical interface.


Syntax for tasks is very simple. Consider an example from a screenshot for the launch of cuckoo:

0 * / 1 * * / Home / Zegi / Bin / Kuku

In total, the task 2 of the main fields: 0 * / 1 * * * - denotes the time when the command will be triggered. A / HOME / ZEGI / BIN / KUKU - the path to the script in which the command (s) is described.

With the address to the script should not be problems (ZEGI is the username ... Do not forget to put your own). But you need to clarify how to set the crown time to execute the script.

Total we have 5 cells for entering, which are separated by a space.
1 - minutes (numbers from 0 to 59)
2 - hours (from 0 to 23)
3 - Day of the month (from 1 to 31)
4 - a month a year (from 1 to 12 ... for example February is 2)
5 - day in weeks (from 1 to 7. The Western week is used, when the beginning is Sunday. Those. Sun-1, Mon-2, W-3, CP-4, Thu-5, PT-6, 7).

Each cell must be completed. If necessary, that the team would be performed every month, then put * In the 4th field. The same applies to the rest of the fields.

Let us come back for example with a cuckoo when the script works every hour. To set frequency used / . For example, if necessary, what would task be performed every 5 minutes, they put on Mondays:

If it is necessary that the task would be performed every hour, it will have to be installed and a certain minute. If you leave the stars (* * / 1 * * *), the crown will perform every minute - for the conditions are observed: he checked all 5 cells and their value meets the current time (minutes - anyway. Watch - every hour, and not just certain ).
Before the slash should always go aside. For example, assign execution every minute, starting with the 30th, scoring 30/1 - it will not work.

If it is necessary that the task is not cyclically, but several times, then the values \u200b\u200bare written through the comma.
For example, you need to perform a task on weekdays at 12 o'clock in the afternoon and 6 pm. It will look like this:

* 12,18 * * 2,3,4,5,6

At the end of the tasks editing, do not forget to save the changes (Ctrl + O\u003e Enter), and then you can exit (Ctrl + x).

CRON should notify that a new task has appeared and it is ready to perform it by writing: "Crontab: Installing New Crontab".

Watch the created tasks (maybe you did not do anything for ourselves, and the network administrator, you can configure anything to you by the team:

And at the end itself script Cuckoowho cuts every hour:

#! / BIN / BASH
H \u003d `Date +% l`
While [$ H -GT 0]
do.
Play ~ / kukushka.wav
H \u003d $ [$ H-1]
Done

Download sound with cuckoo can be team:

wget http://dl.dropbox.com/u/24844052/tuksik/kukushka.wav

Play enters the Sox package. In Ubuntu, you can install it with a command.