Linux system how to use the crond command

  

Linux system crond is mainly used to set the instructions to be executed periodically, such as regular cleaning of the log and other work is very suitable for using the crond command, the following work will introduce you to the Linux crond command How to use it, let's learn together.

a, crond Profile

The concept

crond and crontab concepts are inseparable. Crontab is a command that is commonly found in Unix and Unix-like operating systems. This command reads the instructions from the standard input device and stores them in the “crontab” file for later reading and execution. The word comes from the Greek chronos (χρ?νο?), the original meaning is time. And crond is its daemon.

crond is a daemon used by Linux to periodically perform certain tasks or wait for certain events. Similar to the scheduled tasks under Windows, this service is installed by default when the operating system is installed. Tools, and will automatically start the crond process, the crond process will periodically check whether there are tasks to be executed every minute, if there are tasks to be executed, the task will be automatically executed.

Task scheduling under Linux is divided into two categories, system task scheduling and user task scheduling.

l System task scheduling: The work that the system periodically performs, such as writing cache data to the hard disk, log cleaning, and so on. There is a crontab file in the /etc directory. This is the configuration file for system task scheduling.

The /etc/crontab file includes the following lines:

SHELL=/bin/bash

PATH=/sbin:/bin:/usr/sbin:/usr/Bin

MAILTO=root

HOME=/

# run-parts

01 * * * * root run-parts /etc/cron.hourly02 4 * * * root run-parts /etc/cron.daily22 4 * * 0 root run-parts /etc/cron.weekly42 4 1 * * root run-parts /etc/cron.monthly The first four lines are used to configure crond The environment variable of the task operation, the first line SHELL variable specifies which shell the system should use, here is bash, the second line PATH variable specifies the path of the system execution command, and the third line MAILTO variable specifies the task execution information of crond will pass. The email is sent to the root user. If the value of the MAILTO variable is empty, it means that the task execution information is not sent to the user. The HOME variable of the fourth line specifies the home directory used when executing the command or script. The meanings indicated in lines 6 to 9 will be detailed in the next section. I won’t say more here.

l User task scheduling: The work that users should perform regularly, such as user data backup, timed email reminder, etc. Users can use the crontab tool to customize their own scheduled tasks. All user-defined crontab files are saved in the /var/spool/cron directory. Its file name is the same as the user name.

Second, the use of crontab tools

(1) the use of crontab format

Commonly used crontab format is as follows:

crontab [- u user] [file]

crontab [-u user] [-e| -l| -r | -i]

The options have the following meanings:

l -u user: used to set a user's crontab service. For example, “-u ixdba” indicates setting ixtab user crontab Service, this parameter is generally run by the root user.

l file:file is the name of the command file, which means that file is used as the crontab task list file and loaded into crontab. If this file is not specified on the command line, the crontab command will accept commands typed on standard input (keyboard) and load them into the crontab.

l -e: Edit the contents of a user's crontab file. If you do not specify a user, it means editing the current user's crontab file.

l -l: Display the contents of a user's crontab file. If no user is specified, the current user's crontab file content is displayed.

l -r: Delete a user's crontab file from the /var/spool/cron directory. If no user is specified, the current user's crontab file is deleted by default.

l -i: Give confirmation prompt when deleting the user's crontab file.

(2) The meaning of the crontab file

Each line in the crontab file created by the user represents a task, each field of each line represents a setting, and its format is Divided into six fields, the first five segments are time setting segments, and the sixth segment is the command segment to be executed. The format is as follows:

minute hour day month week command where:

l minute : indicates minutes, which can be any integer from 0 to 59.

l hour: indicates the hour, which can be any integer from 0 to 23.

l day: indicates the date, which can be any integer from 1 to 31.

l month: indicates the month, which can be any integer from 1 to 12.

l week: indicates the day of the week, which can be any integer from 0 to 7, where 0 or 7 represents Sunday.

l command: The command to be executed can be a system command or a script file written by yourself.

In the above fields, you can also use the following special characters:

l asterisk (*): represents all possible values, such as the month field if it is an asterisk, it means that it is satisfied This command is executed every month after the constraints of other fields.

l Comma (,): You can specify a range of values ​​with comma-separated values, for example, “1,2,5,7,8,9”

l -): You can use a middle bar between integers to represent an integer range, for example, “2-6” means “2,3,4,5,6”

l forward slash (/): The interval frequency of the time can be specified with a forward slash, for example, “0-23/2” means that it is executed every two hours. At the same time, the forward slash can be used together with the asterisk, for example */10, if used in the minute field, it means that it is executed every ten minutes.

(3) crontab file example

0 */3 * * * /usr/local/apache2/apachectl restart means to restart the apache service every 3 hours.

30 3 * * 6 /webdata/bin/backup.sh

Indicates that the /webdata/bin/backup.sh script is executed every Saturday at 3:30.

0 0 1,20 * * fsck /dev/sdb8

Indicates that the /dev/sdb8 disk device is checked on the 1st and 20th of each month.

10 5 */5 * * echo “”"/usr/local/apache2/log/access_log means 5th, 10th, 15th, 20th, 25th, 30th of each month The 5:10 of the number performs the cleanup apache log operation.

Third, the use of crontab tools Note

(1) Note the environment variable problem

Sometimes we created a crontab, but this task can not be automatically executed, but manually There is no problem with this task, which is usually caused by the absence of configuration environment variables in the crontab file.

When defining multiple scheduling tasks in a crontab file, one problem that needs special attention is the setting of environment variables. Because we manually perform a certain task, it is performed in the current shell environment. When the environment variable is found, and the system automatically performs task scheduling, no environment variables are loaded. Therefore, all the environment variables required for the task to be run need to be specified in the crontab file, so that the system performs the task scheduling without problems. .

(2) Pay attention to clean up the mail log of the system user

After each task is scheduled to be executed, the system will send the task output information to the current system user via email, so that the log is accumulated. The information can be very large and can affect the normal operation of the system, so it is important to redirect each task.

For example, you can set the following form in the crontab file, ignoring the log output:

0 */3 * * * /usr/local/apache2/apachectl restart 》/dev/null 2 &1“/dev/null 2》&1” means redirecting standard output to /dev/null and then redirecting standard error to standard output, since standard output has been redirected to /dev/null Standard errors are also redirected to /dev/null, so the log output problem is resolved.

(3) System-level task scheduling and user-level task scheduling

System-level task scheduling mainly completes some maintenance operations of the system. User-level task scheduling mainly completes some user-defined tasks. Put the user-level task scheduling into the system-level task scheduling to complete (not recommended), but the reverse is not possible, the root user's task scheduling operation can be set by “crontab –uroot –e” The scheduling task is directly written to the /etc/crontab file. Note that if you want to define a task to restart the system periodically, you must put the task in the /etc/crontab file, even if you create a scheduled restart system under the root user. It is also invalid.

The above is the introduction of the use of the crond command under Linux. It is very convenient to use this command to execute the program regularly. This command is also very simple to learn. Try it out quickly.

Copyright © Windows knowledge All Rights Reserved