crontab、anacron、logrotate relationship

nginx on the server using logrotate to split logs to split a day. But logrotate does not seem to work, the log does not split. Server is CentOS 6.

To find the reason, analyze what could be wrong.
If logrotate is not performed, it may be crond did not start because logrotate is initiated /etc/cron.daily/logrotate script, which you can view the code:

1
2
3
4
5
6
7
8
9
[root@test ~]


/usr/sbin/logrotate /etc/logrotate.conf
EXITVALUE=$?
if [ $EXITVALUE != 0 ]; then
/usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]"
fi
exit 0

It can be seen loading a configuration file logrotate.conf logrotate runs, and this configuration file in addition to set some options related to split logs, the log also includes the division's profile directory /etc/logrotate.d.

nginx log split configuration file is saved in logrotate.d catalog:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@test ~]# cat !$
cat /etc/logrotate.d/nginx
/root/*.log {
Daily
Missingok
rotate 52
compress
delaycompress
notifempty
dateext
create 644 nobody nobody
sharedscripts
postrotate
[ -f /usr/local/nginx/logs/nginx.pid ] && kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`
endscript
}

/root/*.log directory is required to be split logs, the wildcard * for all log files in the directory are divided, split rule is the content of {...}. Here the /root/*.log as nginx logs just to test.
After starting the crond service and found the log or not split, I think of grammar /etc/logrotate.d/nginx will not be a problem with the configuration file, use the following command to debug this file:

1
-vfd logrotate / etc / logrotate.d / nginx   # -vfd three options respectively show details, forced split logs, debugging configuration files rather than just really split logs

The results show that the output of a syntax error, Daily, Missingok should be lowercase. Changed daily, missingok. Again debug configuration files, you can split logs properly:

1
2
3
[Test the root @ ~] LS # -1 / the root / 
the install -2 017 -5 -14. log
the install -2 017 -5 -14. log -20,170,521 #logrotate archive log

The above speculation is crond to execute the script in /etc/cron.daily/, achieve timing to perform scheduled tasks, including the implementation of logrotate log splitting.
In order to verify that the correct, after the online search to find some answers. If there is no crontab command to install:

1
2
3
4
crontabs install yum   # crond installation, crond actually comes from cronie package, which is installed as a dependent crontabs package
chkconfig - the Add crond # Add to the list of boot
chkconfig crond ON # boot crond service
/etc/init.d/crond # crond immediately start

The role of the following files or directories:
cron scheduled task There are two types:

  • 1) system cron task: the crond service execution, / etc / crontab configuration of the system-level tasks
  • 2) User cron task: the crond service execution, with the crontab command to edit user-level tasks
Belonging to the system cron task file or directory:
  • /Etc/cron.d # script task system. Execute rpm -ql cronie can see that the package installation directory is cronie
  • /etc/cron.hourly # hourly execute within the script. Which 0anacron file calls anacron to perform the task, it is the installation package cronie-anacron
  • /etc/cron.daily # script execution within a day. Anacron also be executed within the script, logrotate call the script in the directory within
  • /etc/cron.weekly # script execution within a week.
  • /etc/cron.monthly # monthly execution within the script.

Control execution of the user cron tasks:

  • /etc/cron.allow # default does not exist, if the file exists, only the user can use the crontab command in this file
  • /etc/cron.deny # will not use the crontab command written to the user

Note: cron.allow and cron.deny is a list of user names, user names, one per line. For example, there is a line cron.deny jason, if the effect is currently logged on user is jason, execute crontab -e will be allowed to use crontab command prompt.

The role of the following three directories:

/var/spool/cron/USER_NAME

# This is the file with crontab -e / -l association, the file contains the task crontab -e to edit content

# For example, the implementation of crontab -u root -e, after editing and saving, there will be / var / spool / cron / root this document

/var/spool/anacron/{cron.daily,cron.monthly,cron.weekly}

# These three documents records the time (a day, week or month) on the first performance of anacron

When #anacron task execution, control time here, and decide whether to perform tasks anacron

/var/lib/logrotate.status

# This file is the implementation record logrotate, logrotate refer to this file to determine whether to rotate the log

crontab and anacron and logrotate relationship:

1
2
3
4
5
6
[root @ the Test ~] # all within 01 minutes script execution /etc/cron.hourly cat /etc/cron.d/0hourly # This file specifies hourly
SHELL = / bin / bash
the PATH = / sbin: / bin: / usr / sbin: / usr / bin
MAILTO = root
the HOME = /
01 * * * * root RUN-Parts /etc/cron.hourly # specified by the user to perform tasks root here in, run-parts is actually an executable script, in / usr / bin / run-parts , to execute all scripts within the directory cron.hourly

Description: After each command, the crontab -e finished editing a user's cron settings, cron automatically at / var / spool / cron generates a file with the same name as the user, this user's cron information is recorded in this file. After cron starts every time a bell reading this document, check whether the command to be executed inside. So after modifying this file does not need to restart the cron service. cron service not only to read every minute of all the files in a / var / spool / cron, also need time to read / etc / crontab, so we can configure this file using cron service to do something. Configured with the crontab command is for a user, and edit / etc / crontab is the task for the system. File format of this file is:

1
2
3
4
= SHELL / bin / bash
the PATH = / sbin: / bin : / usr / sbin : / usr / bin # Executable search path
MAILTO = root # If an error occurs, or data output, data as e-mail sent to this account
HOME = / user # running path, this is the root directory
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[root@test ~]# cat /etc/cron.hourly/0anacron   #cron.hourly目录下的脚本,根据条件执行anacron命令
#!/bin/bash
# Skip excecution unless the date has changed from the previous run
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

# Skip excecution unless AC powered
if test -x /usr/bin/on_ac_power; then
/usr/bin/on_ac_power &> /dev/null
if test $? -eq 1; then
exit 0
fi
fi
/usr/sbin/anacron -s
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[root@test ~]# cat /etc/anacrontab   #如果执行anacron命令,那么接着查看anacron的配置文件
# /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 #只有在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

以上anacrontab配置文件最重要的是最后一部分,以这行为例:

1 5 cron.daily nice run-parts /etc/cron.daily

表示每天都执行/etc/cront.daily/目录下的脚本文件,真实的延迟是RANDOM_DELAY+delay。这里的延迟是5分钟,加上上面的RANDOM_DELAY,所以实际的延迟时间是5-50之间,开始时间为03-22点,如果机器没关,那么一般就是在03:05-03:50之间执行。nice命令将该进程设置为nice=10,默认为0,即低优先级进程。如果RANDOM_DELAY=0,那么表示准确延迟5min,即03:05执行cron.daily内的脚本。

1
2
3
4
5
6
7
8
9
[root@test ~]# cat /etc/cron.daily/logrotate  #最后在cron.daily内有logrotate的调用脚本


/usr/sbin/logrotate /etc/logrotate.conf #logrotate将会读取配置文件,最终会读取到/etc/logrotate.d/nginx
EXITVALUE=$?
if [ $EXITVALUE != 0 ]; then
/usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]"
fi
exit 0

当logrotate命令加载了/etc/logrotate.d/nginx配置文件时,还要比较nginx日志的归档日期:

1
2
[root@test ~]# cat /var/lib/logrotate.status | grep /root
"/root/install-2017-5-14.log" 2017-5-21 #如果今天是2017-5-21,这个文件里也是2017-5-21,说明今天已经归档过了,否则就会归档(分割)nginx日志

综上,整个逻辑流程为:

crond服务加载/etc/cron.d/0hourly —>在每小时的01分执行/etc/cront.hourly/0anacron —>执行anacron —>根据/etc/anacrontab的配置执行/etc/cron.daily,/etc/cron.weekly,/etc/cron.monthly —>执行/etc/cron.daily/下的logrotate脚本 —>执行logrotate —>根据/etc/logrotate.conf配置执行脚本/etc/logrotate.d/nginx —>分割nginx日志成功

原文:大专栏  crontab、anacron、logrotate relationship


Guess you like

Origin www.cnblogs.com/petewell/p/11584774.html