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 |
[root@test ~] |
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 |
[root@test ~] |
/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 logrotate.d / nginx |
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 |
[Test the root @ ~] LS # -1 / the root / |
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 |
crontabs install yum # crond installation, crond actually comes from cronie package, which is installed as a dependent crontabs package |
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
- /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 |
[root @ the Test ~] # all within 01 minutes script execution /etc/cron.hourly cat /etc/cron.d/0hourly # This file specifies 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 |
= SHELL / bin / bash |
1 |
[root@test ~]# cat /etc/cron.hourly/0anacron #cron.hourly目录下的脚本,根据条件执行anacron命令 |
1 |
[root@test ~]# cat /etc/anacrontab #如果执行anacron命令,那么接着查看anacron的配置文件 |
以上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 |
[root@test ~]# cat /etc/cron.daily/logrotate #最后在cron.daily内有logrotate的调用脚本 |
当logrotate命令加载了/etc/logrotate.d/nginx配置文件时,还要比较nginx日志的归档日期:
1 |
[root@test ~]# cat /var/lib/logrotate.status | grep /root |
综上,整个逻辑流程为:
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