crond service and usage

crond service

       Crond is a daemon process used to periodically perform certain tasks or wait to process certain events under linux, that is, crontab daemon. By default (every minute), the Crond service will check whether there is a scheduled task that needs to be executed in the system. If there is, it will execute the scheduled task according to the pre-defined rules. There is a crontab file in the /etc directory, this is the configuration file for system task scheduling: 

 

crondtab command

       crontab is a command used 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. Use service crond status to view the status of the cron service. If it is not started, use service crond start to start it:

 

    ◇ The  cron service is a service that is executed regularly. You can add or edit tasks that need to be executed periodically through the crontab command:

       crontab -u //Set a user's cron service, generally root users need this parameter to execute this command

       crontab -l //List the details of a user's cron service

       crontab -r //Delete a user's cron service

       crontab -e //Edit a user's cron service

 

◆How to  use

       There are two commonly used crontab formats as follows:

       crontab [-u user] [file]

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

 

    ◇The  meanings of the options are as follows:

       -u user: used to set the crontab service of a user, for example, "-u home" means to set the crontab service of the home 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 editing the crontab file of the current user.

       -l: Display the content of the crontab file of a certain 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.

 

    ◇  The meaning of crontab file:

       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:

       minute   hour   day  month   week   command其中:

       minute: Indicates the minute, 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: Represents 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, which can be a system command or a script file written by yourself.

 

   ◇  In 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 indicate a range of integers, 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, the forward slash can be used with an asterisk, such as */10. If it is used in the minute field, it means that it will be executed every ten minutes.

 

   ◇  Examples of crontab files:

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

  30 3 * * 6 /webdata/bin/backup.sh means to execute the operation of the /webdata/bin/backup.sh script at 3:30 every Saturday.

  0 0 1,20 * * fsck /dev/sdb8 means to check the /dev/sdb8 disk device on the 1st and 20th of each month.

  10 5 */5 * * echo "">/usr/local/apache2/log/access_log means cleaning up at 5:10 on the 5th, 10th, 15th, 20th, 25th, and 30th of each month Apache log operations.

 

Example

     ◇  Create a cron (the current time will be entered in test.txt every minute):

       1. Log in to linux;
       2. Enter the command crontab -e;
       3. Enter "*/1 * * * * date >> $HOME/test.txt", save and exit;
       4. su root;
       5. cd /etc/ init.d;
       6. ./crond restart.

    Check the results in test.txt, you can see that the current time is recorded in text.txt, and the recording interval is 1 minute, which is consistent with expectations:

 

 

Guess you like

Origin blog.csdn.net/wxt_hillwill/article/details/72909684