crontab、anacron、logrotate relationship

服务器上的nginx使用logrotate来分割日志,设置为每天分割。但是logrotate似乎没有工作,日志并没有分割。服务器是CentOS 6。

为了找到原因,分析可能出错的地方。
如果是logrotate未执行,可能是crond没有启动,因为logrotate被/etc/cron.daily/logrotate脚本所启动,可以查看其中代码:

1
2
3
4
5
6
7
8
9
[root@test ~]# cat /etc/cron.daily/logrotate
#!/bin/sh

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

可以看到logrotate运行时加载配置文件logrotate.conf,而这个配置文件除了设定一些分割日志相关的选项,还包含分割日志的配置文件目录/etc/logrotate.d。

nginx的日志分割配置文件就保存在logrotate.d目录:

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就是需要被分割的日志的目录,通配符*表示目录内的所有log文件都被分割,分割的规则就是{…}中的内容。这里把/root/*.log当做nginx日志只是为了测试。
在启动crond服务后,发现日志还是没有分割,于是想到会不会是/etc/logrotate.d/nginx配置文件的语法有问题,使用以下命令调试这个文件:

1
logrotate -vfd /etc/logrotate.d/nginx  # -vfd 三个选项分别表示显示详情,强制分割日志,只是调试配置文件而不是真的分割日志

输出结果表明有语法错误,Daily,Missingok 都应该是小写。改成daily,missingok。再次调试配置文件,可以正确分割日志:

1
2
3
[root@test ~]# ls -1 /root/
install-2017-5-14.log
install-2017-5-14.log-20170521 #logrotate归档的日志

上面猜测是crond执行/etc/cron.daily/内的脚本,实现定时执行计划任务,包括执行logrotate日志分割。
为了验证是否正确,网上搜索一番后找到了答案。如果没有crontab命令,先安装:

1
2
3
4
5
yum install crontabs  #安装crond,crond实际上来自cronie包,这个包作为crontabs包的依赖被安装
chkconfig --add crond #添加到开机启动列表
chkconfig crond on #开机启动crond服务
/etc/init.d/crond #立即启动crond

以下文件或目录的作用:
cron计划任务有两种类型:

  • 1)系统cron任务:由crond服务执行,/etc/crontab配置系统级别的任务
  • 2)用户cron任务:由crond服务执行,用crontab命令编辑用户级别的任务

属于系统cron任务的文件或目录:

  • /etc/cron.d #系统的任务脚本。执行 rpm -ql cronie 可以看到该目录被cronie包安装
  • /etc/cron.hourly #每小时执行其内脚本。其中的0anacron文件调用anacron来执行任务,它被包cronie-anacron安装
  • /etc/cron.daily #每天执行其内脚本。也被anacron执行其内脚本,logrotate调用脚本就在该目录内
  • /etc/cron.weekly #每周执行其内脚本。
  • /etc/cron.monthly #每月执行其内脚本。

控制用户cron任务的执行:

  • /etc/cron.allow #默认不存在,如果这个文件存在,只有用户在这个文件中才能使用crontab命令
  • /etc/cron.deny #将不可以使用crontab命令的用户写入其中

注意:cron.allow和cron.deny就是用户名的列表,每行一个用户名。比如 cron.deny中有一行jason,效果是如果当前登录用户是jason,执行 crontab -e会提示不允许使用crontab命令。

以下三个目录的作用:

/var/spool/cron/USER_NAME
#这个文件才是跟crontab -e/-l 关联的,这个文件保存了crontab -e编辑的任务内容
#比如执行 crontab -u root -e,编辑保存后,就会有/var/spool/cron/root 这个文件

/var/spool/anacron/{cron.daily,cron.monthly,cron.weekly}
#这三个文件记录了anacron上一次执行的时间(上一天,上一周或上一月)
#anacron任务执行时,对照这里的时间,决定是否执行anacron任务

/var/lib/logrotate.status
#这个文件记录logrotate执行情况,logrotate参考这个文件来决定是否需要rotate日志

crontab和anacron和logrotate的关系:

1
2
3
4
5
6
7
[root@test ~]# cat /etc/cron.d/0hourly     #这个文件指定每小时的01分执行/etc/cron.hourly内的所有脚本
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
01 * * * * root run-parts /etc/cron.hourly #这里的root指定执行任务的用户,run-parts其实是一个可执行脚本,在/usr/bin/run-parts,用来执行cron.hourly目录内的所有脚本

说明:用crontab -e命令每次编辑完某个用户的cron设置后,cron自动在/var/spool/cron下生成一个与此用户同名的文件,此用户的cron信息都记录在这个文件中。cron启动后每过一份钟读一次这个文件,检查是否要执行里面的命令。因此此文件修改后不需要重新启动cron服务。cron服务每分钟不仅要读一次/var/spool/cron内的所有文件,还需要读一次/etc/crontab,因此我们配置这个文件也能运用cron服务做一些事情。用crontab命令配置是针对某个用户的,而编辑/etc/crontab是针对系统的任务。此文件的文件格式是:

1
2
3
4
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin #可执行文件查找路径
MAILTO=root #如果出现错误,或者有数据输出,数据作为邮件发给这个帐号
HOME=/ #使用者运行的路径,这里是根目录
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
10
[root@test ~]# cat /etc/cron.daily/logrotate  #最后在cron.daily内有logrotate的调用脚本
#!/bin/sh

/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日志成功


本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!