The basic task of the crontab setting under Linux

  

First, the introduction of crond crond is a daemon used by linux to periodically perform certain tasks or wait for certain events, similar to the scheduled tasks under windows. After the installation operating system
, the service tool will be installed by default, and the crond process will be started automatically. The crond process will periodically check whether there are tasks to be executed every minute. If there are tasks to be executed, it will be executed automatically. The mission. Task scheduling under Linux is divided into two categories, system task scheduling and user task scheduling.

A. System task scheduling: The work that the system periodically performs, such as writing cached 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/bashPATH=/sbin:/bin:/usr/sbin:/usr/binMAILTO=rootHOME=/# run-parts01 * * * * 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 the environment variables for the crond task to run. 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. The third line MAILTO The variable specifies that the task execution information of crond will be sent to the root user by email. 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 that the command or script is used. Home directory. The meanings indicated in lines 6 to 9 will be detailed in the next section. I won’t say more here. B. User task scheduling: The work that the user 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, crontab tool use

(1) crontab use format

Crontab commonly used format has the following two: crontab [-u user] [file] Crontab [-u user] [-e| -l| -r | The -i] option has the following meanings: * -u user: Used to set a user's crontab service. For example, “-u ixdba” indicates that the ixidba user's crontab service is set. This parameter is generally run by the root user. * 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. * -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: Display the contents of a user's crontab file. If no user is specified, the current user's crontab file content is displayed. * -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. * -i: A confirmation prompt is given when the user's crontab file is deleted.

(2) The meaning of the crontab file In the crontab file created by the user, each line represents a task, and 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: * minute: indicates minutes, which can be any integer from 0 to 59. * hour: indicates the hour, which can be any integer from 0 to 23. * day: indicates the date, which can be any integer from 1 to 31. * month: indicates the month, which can be any integer from 1 to 12. * week: indicates the day of the week, which can be any integer from 0 to 7, where 0 or 7 represents Sunday. * command: The command to be executed can be a system command or a script file that you have written yourself.

Copyright © Windows knowledge All Rights Reserved