26 - cron daemon and crontab command

Lecture



The cron daemon (pronounced “cron”) is a background process that allows you to run files at the scheduled time. If it is necessary that a script or a program is launched at a given periodicity in the system, then this is the task for cron . Today we will look at how to configure the cron daemon manually, as well as using the crontab command.

The cron daemon can run a task not only for the system as a whole, but also for an individual user. Files that describe what and when will be launched for an individual user are located in the / var / spool / cron / crontabs / directory. Only the root user has access to this directory.

Inside the / var / spool / cron / crontabs / directory, a text file is created for each user by the name (login) of the user in which the settings are stored. For example, if there is a user test in the system and he set up cron for himself, then a file with settings will be created - / var / spool / cron / crontabs / test . User test does not have direct access to this file. How this file is created and edited, we will talk later (for this, use the *** and a crontab ).

In order to check whether the cron process is running, execute com *** at:

one
2
igor @ adm-ubuntu: ~ $ ps ax | grep [c] ron
1026? Ss 0:00 cron

If the daemon is not running, you can start it like any other daemon com *** th /etc/init.d/cron start , run as administrator.

The cron daemon runs in per minute mode. That is, you can set the execution time of a particular task with an accuracy of one minute. The main configuration file for the cron daemon is / etc / crontab . The main part of the file is a table in 7 columns, each row, which describes one task. Files that describe tasks for the cron daemon are also often called crontab files.

An example of the / etc / crontab file:

one
2
3
four
five
# mh dom mon dow user command
17 * * * * root cd / && run-parts --report /etc/cron.hourly
25 6 * * * root test -x / usr / sbin / anacron || (cd / && run-parts --report /etc/cron.daily)
47 6 * * 7 root test -x / usr / sbin / anacron || (cd / && run-parts --report /etc/cron.weekly)
52 6 1 * * root test -x / usr / sbin / anacron || (cd / && run-parts --report /etc/cron.monthly)

Each line is given the values ​​of 7 fields:

m - minute - minute - at what minute to perform the task; range of values ​​- (0-59)
h - hour - hour - in which hour to carry out the task; range of values ​​(0-23)
dom - day of month - day of the month - on which days of the month to perform the task; range of values ​​(1-31)
mon - month - month - in which months of the year to perform the task; range of values ​​(1-12)
dow - day of week - the day of the week - on which days of the week to complete the task; range of values ​​(0-7, 0 and 7 - this is Sunday, then in order)
user - user name - user on whose behalf the task will be executed
command - executable com *** a - com *** a, which will be launched for execution at a specified time.

In the first five fields, you can specify not only a number from a range, but also a range of numbers, a list of numbers, a range in increments. The symbol * means any value. You can read more about the values ​​in man 5 crontab . We will consider a few examples.

10 6 * * * root myscript - run com *** in myscript every day, at 6 hours 10 minutes in the morning.
0 5 1 * * root myscript - run com *** in myscript on the first of every month at 5 o'clock in the morning.
30 23 * * 1-5 root myscript - run a com *** in myscript on workdays (except Saturday and Sunday) at 23.30.
0 23 1,3,7,15 1,7 * root myscript - run a com *** for myscript on 1,3,7,15th day, the month of January and July at 23 o'clock.
30 0-23 / 3 * * * root myscript - run com *** u every three hours. That is, the first launch will be at 0:30, then at 3:30, etc.
* / 5 * * * * root myscript - run a com *** u every 5 minutes (* / 5 - no spaces).

Be careful with fields 3 and 5 - the day of the month and the day of the week. If there are numbers in both fields, for example, * 6 5 * 1 root myscript , then writing means starting the task at six o'clock on the 5th of every month or every Monday, but not every Monday of the 5th.

crontab files can also be contained in the /etc/cron.d/ directory, which is viewed by the cron daemon in the process. They can have arbitrary names, but the structure should be the same as the / etc / crontab file. Usually, the directory stores the tasks of various services and programs (the files are then named for the names of the services or programs).

Regular users can also create their crontab files and edit them, if the opposite is not prohibited by the administrator. To edit crontab files, use the *** and crontab com. Log in to the system as a regular user account (for example, test ) and type the *** at crontab -e . You will be taken to the editor (the default text editor on your system) in which you will need to type the task. Note that in the user crontab files there is no user field, since it is calculated by the cron daemon from the name of the file itself, which after editing will be saved in the / var / spool / cron / crontabs / directory under the name test . Type in the line with the task, for example:

29 12 * * * touch testfile

In the first second field, specify the current time for you (several minutes more than the current one) and save the file. Then, after the scheduled time, check the root directory of the test user and verify that the file was created:

one
2
test @ adm-ubuntu: ~ $ ls -l [0-9] *
-rw-r - r-- 1 test test 0 2010-01-11 12:16 testfile

As you can see the testfile file has been created. Also from under the administrator account you can verify that the file / var / spool / cron / crontabs / test was created :

one
2
igor @ adm-ubuntu: /etc/cron.d$ sudo ls -l / var / spool / cron / crontabs /
-rw ------- 1 test crontab 266 2010-01-11 12:13 test

If it is necessary to prohibit the user to create and edit crontab files, this can be done by writing his account name to the /etc/cron.deny file. If the file does not exist, then you need to create it yourself. Account names are recorded one per line. You can also use the /etc/cron.allow file. Details are described in man crontab .

Cron job start values

Sometimes there are moments when you need to specify a kind of script execution time (for example, only at boot). For this, reserved words come to the rescue:

Строка Значение
------ -------
@reboot Запуск при загрузке
@yearly Запуск каждый год, "0 0 1 1 *".
@annually (тоже самое, что @yearly)
@monthly Запуск раз в месяц, "0 0 1 * *".
@weekly Запуск раз в неделю, "0 0 * * 0".
@daily Запуск раз в день, "0 0 * * *".
@midnight (тоже самое, что @daily)
@hourly Запуск раз в час, "0 * * * *".

Example

@reboot root /usr/local/etc/rc.d/starts

Note.

They work on Linux / FreeBSD , but they don't work on Solaris.

created: 2014-09-13
updated: 2021-03-13
132510



Rating 9 of 10. count vote: 2
Are you satisfied?:



Comments


To leave a comment
If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

LINUX operating system

Terms: LINUX operating system