Cron is a task scheduler that runs on Unix\Linux hosting. It allows you to automatically perform certain actions on the server (launching programs, scripts, etc.), with given time or frequency.

Correctly configuring the Cron job scheduler

Jobs for Cron can be described as follows - these are several lines (one line - one job), which indicate the frequency of launch and the command (which means what actually needs to be launched):

30 3 * * 2 /yourdirectory/myscript.pl

Schematically:

Minutes Hours DayMonth Month DayWeek Command

minutes- set by a number from 0 to 59

Watch- set by a number from 0 to 23

Day of the Month- set by a number from 1 to 31

Month- set by a number from 1 to 12

Day of the Week- 1 - Monday, 2 - Tuesday, 3 - Wednesday, 4 - Thursday, 5 - Friday, 6 - Saturday, 7 - Sunday

Thus, in our example (30 3 * * 2 /yourdirectory/myscript.pl) back means that you need to run the file /yourdirectory/myscript.pl every Tuesday at 3:30 am

Also in each of the fields we can use:

Writing separated by commas: 2,5,16 - if you write this in the Hours field, the task will run at 2 am, 5 am and 4 pm.

Interval: 5-9 - if you write this in the Minutes field, the task will run every minute in the period from 5 to 9 minutes.

Additional frequency: /4 - if you write this in the hours field, it will mean that the launch will occur every 4 hours.

Important! An asterisk (*) means all possible values! Thus, an inexperienced webmaster who decides that to run a task on the 1st of every month it is enough to write * * 1 * * /yourdirectory/myscript.pl stumbles upon the fact that the task will be launched every minute, every hour.

How and where to enter Cron jobs?

The first way to work with Cron is through your hosting control panel. But in different panels, configuration and management are carried out differently:

cPanel: Control Panel -> Cron Jobs

ISP Manager: Control Panel -> Scheduler (Cron)

Parallels Plesk: Control Panel -> Scheduled Tasks

If your hosting provider does not have the ability to work with jobs through the control panel, then all work with Cron is usually done through the SSH protocol. Everything is simple here - connect to the server via SSH and enter commands. To connect to the server use free program PuTTY (how to set it up), and the commands are entered on the command line.

To get started, enter the command

After this, you will most likely be transferred to the vi text editor (different providers may have different editors). vi is a fairly complex editor, so we recommend that you try entering the line

If a lighter editor starts, then everything is fine, but if your hosting provider does not support it, then Google will help you figure it out with vi. We will just briefly outline the main points:

Entering text- press the i key and the editor will switch to text input mode;

exit text input mode- Esc

Delete character- x (if you are in test input mode, then to delete a character, first press Esc and then x);

Important! When entering jobs for Cron, you must press Enter after each line, even if this is the only line.

To watch already existing jobs for Cron enter crontab -l

Cron jobs with useful examples

See the rules for creating Cron jobs. Run a task every 2 hours at 0 minutes (every day, every month)

0 */2 * * * /yourdirectory/myscript.pl

Run the task every time after server reboot

@reboot/yourdirectory/myscript.pl

Run the task on Wednesdays at 3:20 am (every day, every month)

20 3 * * 3 /yourdirectory/myscript.pl
1 0 14 3 * /yourdirectory/myscript.pl

Run the task monthly on the 1st at 3:15 am (every month)

15 3 1 * * /yourdirectory/myscript.pl

Run PHP files on a schedule using Cron

To run PHP scripts via Cron, you can use a special interpreter. Unfortunately, we cannot give you instructions on how to use it, because... Different providers may use different software. Therefore, many webmasters launch PHP files using WGET, for this we use the following crontab entry:

30 3 * * 2 root wget -O - -q -t 1 http://mysite.com/file.php

"-O -" means that Cron will not create extra files, but will work through the console. This allows you to avoid cluttering the server.

"-q" disables printing the operation to the screen

"-t 1" only allows one connection attempt.

http://mysite.com/file.php - path to your PHP file (it is not necessary to specify an absolute path).

Important! If you pass parameters using this PHP file, there are times when WGET does not process them quite correctly. In this case, we recommend enclosing the PHP file address in single quotes:

30 3 * * 2 root wget -O - -q -t 1 "http://mysite.com/file.php"

Also, there is another way to start:

30 3 * * 2 /usr/bin/wget -O - -q -t 1 http://mysite.com/file.php

But in this case, you need to know the path to the wget folder on your server (most often /usr/bin/wget or /usr/local/bin/wget).

Receive a Cron work report by email

Cron can be configured to send emails with the results of running jobs. This feature can also be useful for notification in case of an error. To receive a report by e-mail, you need to edit crontab (before tasks) and write the following line:

Where [email protected]- this is the e-mail to which letters will be sent. You can also add multiple addresses, separated by commas.

There is one more thing: you will also receive the result of the script by email. for example, if the script writes some kind of inscription on the screen, then the same inscription will be sent to your email along with the report. If this annoys you, then add the line > /dev/null 2>&1 at the end of the task

30 3 * * 2 /usr/bin/wget -O - -q -t 1 http://mysite.com/file.php > /dev/null 2>&1

Suppose we need to run a certain task every day, or maybe every Saturday at 12 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 in the OS Linux Debian 6.0 squeeze, but will work on other Unix-like operating systems in most cases.

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 current user;
-l - shows a list of current tasks;
-e - launches the task scheduler editor;
-r - deletes all current tasks.

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

cron syntax

In general, a cron task is a line like:

* * * * * team

Each asterisk in a line corresponds to a specific value:

0 and 7 in the day of the week indicate Sunday, since in some countries the day of the week begins 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 character 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 - when loading 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.

Example 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'm user's crontab file and this is what my master taught me
#I say hello to him after my download
@reboot echo "Hello, Master!"

#Every weekday at 6:45 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 day off,
#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 working day
0 18 * * 1-5 echo "Come home, Master" | mail -s "End of working day" user

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

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

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

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

To create or change a custom schedule, enter the command:

Crontab -e

When you launch it for the first time, the utility will prompt you to select an editor; we recommend choosing mcedit(requires mc installed), or another editor with which you know how to work.

The format of the schedule lines is:

minute hour day month day_of_week command

  • Minute- time in minutes from 0 to 59
  • Hour- from 0 to 23
  • Day- day of the month from 1 to 31
  • Month- from 1 to 12 or letter 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 that will be executed, writing like team1 && team2 to run several commands in a row.

The values ​​of minutes, hours, days can be specified as follows:

  • Meaning- a number indicating a date or time, a wildcard is allowed * allowing full range of values
  • Multiple values- it is allowed to specify several values ​​separated by commas, for example 2,14,22
  • Range of values- indicated with a hyphen, for example 2-10
  • Value step- indicated through a fraction, the denominator of which is a step, for example */3 - every third value 0, 3, 6, 9, etc. The numerator must be a range of values ​​or an asterisk.

Consider the following example entry:

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, run the test script in Ivanov’s home directory.

We would like to immediately warn you against a common mistake: when indicating periodic execution, all dates must be indicated explicitly, the asterisk indicates the full range of values, and not their absence. For example, if you need to execute a certain script every hour from 10 to 15, it would be incorrect:

* 10-15 * * * /home/ivanov/test

This line will run the script every minute in the range from 10 to 15 hours. It will be right:

0 10-15 * * * /home/ivanov/test

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

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

  • @reboot- execute command on reboot
  • @yearly or @annually- execute on January 1, similar to the entry: " 0 0 1 1 * "
  • @monthly- perform on the 1st of every month, similar to " 0 0 1 * * "
  • @weekly- perform every Sunday, equivalent to " 0 0 * * 0 "
  • @daily or @midnight- every day at midnight" 0 0 * * * "
  • @hourly- once an hour, " 0 * * * * "

So for daily execution of our script every midnight you can write:

@midnight /home/ivanov/test

Having completed creating the schedule, save the file and exit the editor. The custom schedule will be saved in /var/spool/cron/crontabs under the name of the current user.

A file is provided for system and administrative tasks /etc/crontab The syntax of entries in it is distinguished by the presence of an additional value - the user on whose behalf the task will be launched:

minute hour day month day_of_week user command

An example of such an entry:

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 editing it should be done with caution. All system and administrative tasks should be placed there.

As we see cron It is quite 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 master this tool.

In system Linux automatic starting of jobs is performed by the crond daemon, and scheduling for automatic start Not only the system administrator, but also users can perform tasks.

How does crond work?

The principle of the crond daemon is simple. After starting (usually when the system boots), the daemon wakes up every minute and checks to see if any programs are scheduled to run at that minute. If such programs are found, the daemon starts them and sends them via e-mail messages to users who have scheduled a launch.

Scheduling tasks

Creating a schedule is not a difficult task. The schedule is located in a separate crontab file. Each line of the file contains a task that should be run in certain time.

Crontab File Format

The time-date part consists of five numeric fields, separated by spaces, that define the time at which the job runs:

For the convenience of filling out these fields, the following rules have been introduced:

  • You can specify values ​​as numerical intervals. For example, the interval 1-3 in the hour field means 1.00, 2.00 and 3.00 midnight, 2-4 in the day of week field - Tuesday, Wednesday or Thursday.
  • Intervals can be set in increments greater than one. For example, to indicate every second hour starting at midnight, you would set the interval to 0-23 in increments of 2 separated by a slash: 0-23/2
  • An asterisk (*) indicates the full range of field values ​​- from minimum to maximum. For example, in the day of the month field the asterisk means the interval 0-31, in the day of the week field - 0-7
  • The day of the week or month can be indicated by the first three letters of its (English) name

Time-date examples

Several examples of filling out time-date fields:

0 1 * * * Run the task daily at 1.00 midnight 30 14 * * 0 Run the task on Sundays at 2.30 pm 0 23-7/2.8 * * * Run the task every 2 hours from 23.00 to 7.00 and at 8.00 0 12 * 1 mon Run the job at noon on every Monday in January of each year 0 12 2 feb * Run the job at noon on the second of February each year

Command field

The command field is separated from the date-time fields by one or more spaces and extends to the end of the line. Commands are processed by the /bin/sh shell.

For example, the following crontab entry calls for running /usr/sbin/backup at 1:00 a.m. every day:

0 1 * * * /usr/sbin/backup

Some commands (such as mail) require input from the standard input device. This is indicated using a percent sign (%). The first such character indicates the beginning of standard input, each subsequent character indicates a line change.

Editing the crontab file

The crontab file is edited with the crontab -e command. Two approaches are possible:

  • creating a new file with all the entries that should be included in the crontab file, and then writing it to disk under this name using the crontab command;
  • directly editing the file using the crontab -e command.

Write from file

To write the contents of a crontab file from another file, you need to create that other file (in the usual text editor) with all the entries that should make up the crontab file. This could be, for example, the following entry:

0 1 * * * /usr/sbin/backup

The created file should be assigned suitable name, for example сron jobs. After specified file created, its contents must be written to the crontab file with the command:

~$ crontab cronjobs

The contents of the cronjobs file will completely replace the contents of the crontab file given user. Using this method, any user can edit their crontab file. The superuser has special rights to edit the crontab files of other users. Editing someone else's file is indicated by the -u flag. For example, by command:

~# crontab -u oleg cronjobs

the cronjobs file is written as the crontab file of user oleg.

Editing the crontab file directly

The crontab command avoids the multi-step operation of creating separate file. If you issue the crontab command with the -e option, you can edit the crontab file directly.

By default, using the crontab command with the -e option, the crontab file is loaded into the editor. Editor Vi- a powerful, albeit complex tool, popular among experienced users Unix. Someone who prefers a different editor, e.g. Xedit, can install corresponding value environment variable EDITOR:

~$ export EDITOR=xedit

After this, enter the command

causes the crontab file to be opened in the specified editor.

Just as a normal user can edit their own crontab file, a superuser can edit other users' crontab files. To do this, use the command:

~# crontab -u user-name -e

Viewing the crontab file

To view the contents of the crontab file, enter the command:

The superuser can view the crontab files of other users:

~# crontab -u username -l

Removing the crontab file

To delete the contents of their crontab file, the user enters the command:

The superuser can delete other users' crontab files.

(Scheduled tasks) is used to configure the execution of commands on a schedule or at a strictly designated time. By using CronTab You can set a specific time, and the command you specify will be executed exactly at that time. 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 perform the task once an hour for the first 2 hours, then not perform it for the next 2 hours.

Also, one of the conveniences is the ability to send the results of completing tasks by email. To do this, click on the corresponding inscription and enter your email.

Task Master

First acquaintance with Cron for us it starts with discovery Quest Masters. Using the wizard, you can configure the execution time of scripts without any serious administration knowledge by simply selecting a specific combination from the drop-down lists. You must also enter the path to the script or command that will be executed in Cron.

Let's see an example of running a script written in the language PERL, which needs to be launched every 5 minutes.

After clicking the button, under the main window Cron will appear new entry with your first configuration. The number of such entries is unlimited.

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

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

Manual setup of CronTab.

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

Time, interval

Minutes can be from 0 before 59
Hours may be from 0 before 23
The day of the month can be from 1 before 31
A month maybe 1 before 12
The day of the week can be from 0 before 7 , where 0 and 7 are Sunday

Can be configured CronTab to perform tasks not only at a certain time
time, but also every minute, hourly, daily, weekly or monthly,
using a combination */x

Examples

*/5 * * * * - run the 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- running the php script test.php every minute
0 */1 * * * /usr/bin/perl ~/site.ru/public_html/test.pl- running the perl script test.pl every hour

Team

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