Linux basic study notes-routine work

Routine work

1. Routine work performed in cycles

1.1. User settings

The restricted data used are:

  • /etc/cron.allow

    Write the accounts that can use crontab into it. Users who are not in this file cannot use crontab;

  • /etc/cron.deny

    Write the accounts that cannot use crontab into it. If users who are not recorded in this file, crontab can be used.

/etc/cron.allow has a higher priority than /etc/cron.deny ! Generally speaking, it is sufficient to keep one of the two.

When the user uses the crontab command to establish a work schedule, the work will be recorded in /var/spool/cron/ , and the account is used as the judgment!

Syntax of crontab:

[root@li ~]# crontab [-u 账号] [-l|-e|-r]
选项与参数:
-u:只有 root 才能使用的参数,帮助其他使用者建立/移除 crontab 工作
-e:编辑 crontab 的工作内容
-l:查阅 crontab 的工作内容
-r:移除所有的 crontab 的工作内容,若仅要移除一项,请用 -e 编辑
#用 li 的身份在每天的 12:00 发邮件给自己
[li@li ~]$ crontab -e
0 12 * * * mail -s "at 12:00" li < /home/li/.bashrc

The six fields of the above edit content respectively indicate:

Representative meaning minute hour date month week instruction
Number range 0-59 0-23 1-31 1-12 0-7 instruction

What's more interesting is that "week", 0 and 7 both represent Sunday. In addition, there are some auxiliary characters:

Special characters Description
* It means accept at any moment!
, Represents the meaning of divided time periods
- Represents a period of time
/n n is a number, representing every period of time

1.2. System configuration files: /etc/crontab, /etc/cron.d/*

This "crontab -e" is designed for the user's cron. What should I do if it is " routine work of the system "? Do you still need to use "crontab -e" to manage it? Of course not, you only need to edit the file /etc/crontab . One thing to note is that the crontab -e crontab is actually the executable file /usr/bin/crontab , but /etc/crontab is a text file.

[root@li ~]# cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root		#若有额外的 STDOUT,以 email 将数据发给谁

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed

1.2.1, the location where the crond service reads the configuration file

Generally speaking, crond has three preset execution script configuration files:

  • /etc/crontab
  • /etc/cron.d
  • / var / spool / cron / *

Among the three places, the files in the /etc/crontab file and the /etc/cron.d/* directory are more related to the operation of the system, and the other is the configuration file that is more related to the user's own work. Just put it in /var/spool/cron/. Now let's look at the files in /etc/cron.d:

[root@li cron.d]# ll /etc/cron.d
总用量 4
-rw-r--r--. 1 root root 128 8月   9 2019 0hourly
[root@li cron.d]# ls /etc/cron.*
/etc/cron.deny

/etc/cron.d:
0hourly

/etc/cron.daily:
logrotate  man-db.cron

/etc/cron.hourly:
0anacron

/etc/cron.monthly:

/etc/cron.weekly:

[root@li cron.d]# cat /etc/cron.d/0hourly
# Run the hourly jobs
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
01 * * * * root run-parts /etc/cron.hourly

Please pay attention to the last line, every hour on the hour will execute the run-parts /etc/cron.hourly command. So what are run-parts? If you analyze this executable file, you will find that it is a shell script. The run-parts script will randomly select a time to execute all the executable files in the /etc/cron.hourly directory within about 5 minutes! Therefore, the file placed in /etc/cron.hourly/ must be a command script that can be directly executed.

There are cron.daily/, /etc/cron.monthly, /etc/cron.weekly under /etc. The difference between these three files and /etc/cron.hourly is that these three directories are executed by anacron Yes, and the execution method of anacron is placed in /etc/cron.hourly/0anacron.

Summarized as follows:

  • Use crontab -e for personalized behavior: If you build a routine job based on personal needs, it is recommended to directly use crontab -e to build your job! This will also ensure that your command behavior is not seen by others.
  • Vim /etc/crontab for system maintenance and management: If your routine work is an important task of the system, in order to make your own management convenient and easy to track, it is recommended to write directly to /etc/crontab.
  • The software developed by myself uses vim /etc/cron.d/newfile.
  • Fixed special tasks performed hourly, daily, weekly, and daily: If it is related to system maintenance, it is recommended to place it in /etc/crontab.

2. Can wake up work tasks during shutdown

2.1 What is anacron

Anacron is not used to replace crontab. The purpose of anacron is to deal with the execution of crontab in Linux systems that are not started 24 hours a day! As well as routine work that has exceeded time due to some reasons and has not been performed .

2.2, anacron and /etc/anacrontab

Anacron is actually a program rather than a service.

[root@li cron.d]# cat /etc/cron.hourly/0anacron
#!/bin/sh
# Check whether 0anacron was run today already
if test -r /var/spool/anacron/cron.daily; then
    day=`cat /var/spool/anacron/cron.daily`
fi
if [ `date +%Y%m%d` = "$day" ]; then
    exit 0;
fi

# Do not run jobs when on battery power
if test -x /usr/bin/on_ac_power; then
    /usr/bin/on_ac_power >/dev/null 2>&1
    if test $? -eq 1; then
    exit 0
    fi
fi
/usr/sbin/anacron -s

The basic syntax of anacron:

[root@li cron.d]# anacron [-sfn] [job]...
[root@li cron.d]# anacron -u [job]...
选项与参数:
-s:开始一连续的执行各项工作(job),会依据时间记录文件的数据判断是否进行
-f:强制进行,而不去判断时间记录文件的时间戳
-n:立刻进行未进行的任务,而不延迟等待时间
-u:仅更新时间记录文件的时间戳,不进行任何工作
job:由 /etc/anacrontab 定义的各项工作

Anacron's configuration file:

[root@li cron.d]# cat /etc/anacrontab
# /etc/anacrontab: configuration file for anacron

# See anacron(8) and anacrontab(5) for details.

SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# the maximal random delay added to the base delay of the jobs
RANDOM_DELAY=45		#随机给予最大延迟时间,单位是分钟
# the jobs will be started during the following hours only
START_HOURS_RANGE=3-22		#延迟多少个小时内应该要执行的任务时间

#period in days   delay in minutes   job-identifier   command
1       5       cron.daily              nice run-parts /etc/cron.daily
7       25      cron.weekly             nice run-parts /etc/cron.weekly
@monthly 45     cron.monthly            nice run-parts /etc/cron.monthly
[root@li cron.d]# more /var/spool/anacron/*		#时间戳文件
::::::::::::::
/var/spool/anacron/cron.daily
::::::::::::::
::::::::::::::
/var/spool/anacron/cron.monthly
::::::::::::::
::::::::::::::
/var/spool/anacron/cron.weekly
::::::::::::::

Let's take the setting of the /etc/cron.daily line to illustrate. The meaning of the four fields:

  • Number of days: The number of days between the current execution of anacron and the time stamp (time record file in /var/spool/anacron/). If it exceeds this number of days, it is ready to start execution. If it does not exceed this number of days, no subsequent instructions will be given;
  • Delay time: If it is determined that the routine work has to be performed after the number of days is exceeded, please delay the execution time, because you are worried that there will be other resource conflicts when starting immediately;
  • Job name definition;
  • The actual instruction to be executed.

According to the content of the coordination file above, we probably know that the execution process of anacron should be like this:

  1. The number of days from the analysis of /etc/anacrontab to the name of cron.daily is 1 day;
  2. From /var/spool/anacron/cron.daily, retrieve the timestamp of the last execution of anacron;
  3. Comparing the previous step with the current time, if the difference in days is more than 1 day (including 1 day), it is ready to execute the instruction;
  4. If you are ready to make a command, it will be delayed by 5 minutes + 3 hours according to the settings of /etc/anacrontab;
  5. After the delay time has elapsed, the subsequent instructions will be executed, namely run-parts /etc/cron.daily;
  6. After execution, the anacron program ends.

Finally, we summarize:

  1. crond will actively read /etc/crontab, /var/spool/cron/*, /etc/cron.d/* and other configuration files, and execute it according to the time setting of "minute, hour, day, month, and week" Various work arrangements;
  2. According to the settings of /etc/cron.d/0hourly, actively go to the /etc/cron.hourly directory and execute all the executable files in the directory;
  3. Because of the script file /etc/cron.hourly/0anacron, actively execute anacron every hour and call the configuration file of /etc/anacrontab;
  4. According to the setting of /etc/anacrontab, analyze the execution files in /etc/cron.daily, /etc/cron.weekly, /etc/cron.monthly on a daily, weekly, and monthly basis to perform a fixed cycle. Instructions.

In other words, if the action you need to perform every Sunday is placed in /etc/crontab, then the action will expire as long as it expires, and will not be caught back and executed again. But if it is placed in the /etc/cron.weekly directory, then the work will be performed regularly, almost certainly once in a week. If you shut down for more than a week, the job will be executed actively within a few hours after you turn it on!

Guess you like

Origin blog.csdn.net/qq_36879493/article/details/108033044