Timing task (crontab)

crontab

The crontab command is commonly used in Unix and Unix-like operating systems to set instructions to be executed periodically. This command reads 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 (χρνο), which originally means time. Usually, the instructions stored in crontab are activated by the daemon, and crond often runs in the background, checking every minute whether there is a scheduled job to be executed. Such jobs are generally called cron jobs.

Insert picture description here
In the crontab file created by the user, each line represents a task, and each field of 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:

f1 f2 f3 f4 f5 program
minute hour day month week command
  • Where f1 is the minute,
    f2, the hour,
    f3, the day of the
    month , f4, the month,
    f5, the day of the week,
    program, the program to be executed.

  • When f1 is *, it means that the program must be executed every minute, when f2 is *, it means that the program must be executed every hour, and so on

  • When f1 is ab, it means that it will be executed from the a-th minute to the b-th minute. When f2 is ab, it means that it will be executed from the a-th hour to the b-th hour, and so on

  • When f1 is */n, it means that it will be executed every n minutes, f2 is */n, it means that it will be executed every n hours, and so on.

  • When f1 is a, b, c,..., it means that it will be executed in minutes a, b, c,..., and when f2 is a, b, c,..., it means that it will be executed in hours a, b, c...

Users can also store all the settings in a file first, and use crontab file to set the execution time.

  • Instructions

Insert picture description here

Detailed explanation of crontab command

  • Command format:
    crontab[-uuser]file
    crontab[-uuser][-e|-l|-r]

  • Command function:
    Through the crontab command, we can execute the specified system command or shellscript script at a fixed interval. The unit of the time interval can be any combination of minutes, hours, days, months, weeks and above. This command is very suitable for periodic log analysis or data backup.

  • Command parameters:
    -uuser:
    used to set the crontab service of a user, for example, "-uixdba" means to set the crontab service of the ixdba user, 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 task list file of crontab 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 crontab.

  • -e:
    Edit the content of a user's crontab file. If you do not specify a user, it means to edit the crontab file of the current user.

  • -l:
    Display the content of the crontab file of a user. If the user is not specified, it means that the content of the crontab file of the current user 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 will be deleted by default.

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

Common method

  • Create a new crontab file

Insert picture description here

  • List crontab files

In order to list the crontab files, you can use: make a backup of the crontab files in the HOME directory: HOME/mycron. In this way, once you accidentally delete the crontab files by mistake, you can quickly restore them using the method described in the previous section.

  • Edit crontab file
  • Delete crontab file

Insert picture description here

  • Recover lost crontab file

Insert picture description here

file

  • The crontab file contains a series of jobs and instructions sent to the cron daemon. Each user can have his own crontab file; at the same time, the operating system saves a crontab file for the entire system, which is usually stored in a subdirectory under /etc or /etc, and this file can only be used by the system administrator modify.

  • Each line of the crontab file follows a specific format, separated into several fields by spaces or tabs, and each field can be placed with a single or multiple values. What is the usage of crontab format?

Grammar introduction

  • Access:
    root user and owner of crontab file

  • 语法:
    crontab [-e [UserName]|-l [UserName]|-r [UserName]|-v [UserName]|File ]

  • Explanation:
    crontab is used to allow users to execute programs at fixed times or intervals, in other words, it is similar to the user's schedule.
    -u user refers to setting the schedule of the specified user. The premise is that you must have the authority (for example, root) to be able to specify the schedule of others. If you do not use -u user, it means to set your own schedule.

  • Parameter
    -e [UserName]: Run a
    text editor to set the schedule. The default text editor is VI. If you want to use another text editor, please set the VISUAL environment variable first to specify which text editor to use (For example, setenv VISUAL joe)
    -r [UserName]:
    delete the current schedule
    -l [UserName]:
    list the current schedule
    -v [UserName]:
    list the status of the user's cron job

  • Note that
    when the program is executed at the time you specify, the system will send you a letter showing the contents of the program. If you do not want to receive such a letter, please add> / after a blank space on each line dev/null 2>&1 is fine.
    % Is considered as newline in crontab, so use \ to escape. For example, in the execution line of crontab, if there is "date +%Y%m%d", it must be replaced with: "date +%Y%m%d"

  • Output configuration in crontab

  • In crontab, the output of running script is often configured as: >/dev/null 2>&1 to avoid output of content during crontab operation.

The result of the shell command can be defined in the form of'>'. The output
/dev/null represents an empty device file
(>) The greater than sign represents where to redirect, for example: echo “123”> /home/123.txt
1 represents the stdout standard Output, the system default value is 1, so ">/dev/null" is equivalent to "1>/dev/null"
2 means stderr standard error
& means equivalent, 2>&1 means that the output redirection of 2 is equivalent to 1
then the meaning of the statement to redirect output:
1> / dev / null first standard output is redirected to the null device file, which is not output any information to the terminal, does not display any information.
2>&1 means that standard error output redirection is equivalent to standard output, because standard output has been redirected to an empty device file before, so standard error output is also redirected to an empty device file.

Extension (simple comparison between at command and crontab)

  • The simple difference and usage of crontab and at.
    Both commands can be used to submit jobs,
    but crontab is mainly used to submit jobs
    that are executed continuously and at is used to submit jobs that are executed after a period of time (the entire job is automatically deleted after execution)

Our timed task is over here~ _~

Guess you like

Origin blog.csdn.net/qq_49296785/article/details/108879481