The use of crontab in Linux commands

1. Introduction to Crond

Crond is a daemon process used to periodically perform certain tasks or wait for certain events under linux. It is similar to scheduled tasks under windows. When the operating system is installed, this service tool will be installed by default and will be started automatically The crond process, the crond process will periodically check whether there is a task to be executed every minute, and if there is a task to be executed, the task will be executed automatically.

In addition, because users can also set up scheduled tasks themselves, the Linux system also provides commands for users to control scheduled tasks: crontab command.

Two, task classification

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

1. System task scheduling:

The tasks that the system needs to perform periodically, such as writing cached data to the hard disk, cleaning logs, etc. There is a crontab file in the /etc directory, which is the configuration file for system task scheduling.

Insert picture description here
The first four lines are used to configure the environment variables for running the crond task. The shell variable in the first line specifies which shell the system will use, here is bash; the PATH variable in the second line specifies the path for the system to execute the command; the MAILTO variable in the third line The task execution information with crond specified will be sent to the root user via email. If the value of the MAILTO variable is empty, it means that the task execution information will not be sent to the user; the HOME variable in the fourth line specifies the command or script used when executing Main directory.

2. User task scheduling

The tasks that users need to perform regularly, such as user data backup, regular email reminders, etc. Users can use crontab to customize their own scheduled tasks. All user-defined crontab files are stored in the /var/spool/cron directory, and their file names are consistent with the user names.

The meaning of crontab file:

In the crontab file created by the user, each line represents a task, and each field in each line represents a setting. Its format is divided into six fields. The first five sections are the time setting section, and the sixth section is The command segment to be executed, the format is as follows:

minute hour day month week command

Insert picture description here
In each of the above fields, the following special characters can also be used:

Asterisk (*): Represents all possible values. For example, if the month field is an asterisk, it means that the command operation will be executed every month after meeting the constraints of other fields.

Comma (,): You can specify a list range with comma-separated values, for example, "1,2,5,7,8,9"

Middle bar (-): A middle bar between integers can be used to represent an integer range, for example, "2-6" means "2,3,4,5,6"

Forward slash (/): You can use forward slashes to specify the time interval frequency, for example, "0-23/2" means that it will be executed every two hours. At the same time, forward slashes can be used with asterisks, such as */10. If used in the minute field, it means that it will be executed every ten minutes.

Three, crond service

Install crontab:

yum install crontabs

Service operation instructions:

/sbin/service crond start //启动服务

/sbin/service crond stop //关闭服务

/sbin/service crond restart //重启服务

/sbin/service crond reload //重新载入配置

/sbin/service crond status //启动服务

Check whether the crontab service has been set to start at boot, execute the command:

ntsysv

Join to automatically start at boot:

chkconfig –level 35 crond on

Four, detailed explanation of crontab command

parameter meaning Example
-l(list) View the contents of the crontab file crontab -l
-e (edit) Edit the contents of the crontab file crontab -e
-i Delete the content of the crontab file, it will prompt for confirmation before deleting crontab -i
-r Delete the contents of the crontab file crontab -r
-u usr Perform the tasks used by the user crontab -u

crontab {-l | -e} is actually operating files like /var/spool/cron/ current user (pwd is viewing root, then /var/spool/cron/root is operating).

Reprinted
https://www.cnblogs.com/intval/p/5763929.html
https://www.cnblogs.com/liang-io/p/9596294.html
https://blog.csdn.net/lzghxjt/article /details/80375626

Guess you like

Origin blog.csdn.net/qq_43458555/article/details/108171756