CentOS 6、CentOS 7 ,使用crontab和logrotate实现Nginx日志轮询(转存)

CentOS 6、CentOS 7 ,使用crontab和logrotate实现Nginx日志轮询(转存)

1、安装crontab
2、安装logrotate日志文件管理工具
3、分析cron.daily
4、Nginx日志轮询(转存)配置,Apache的类似
5、为什么日志轮询(转存)生成日志的时间是凌晨三四点
6、立即执行logratate
7、logratate常用配置参数

1、安装crontab
if ! which crond >/dev/null 2>&1;then yum install cronie -y; fi
systemctl enable crond

2、 安装logrotate日志文件管理工具
yum -y install logrotate
查看logrotate的软件包的信息
rpm -ql logrotate
/etc/cron.daily/logrotate
/etc/logrotate.conf
/etc/logrotate.d
.....
.....

3、分析cron.daily
cd /etc/cron.daily
[root@www.zhangfangzhou.cn cron.daily]# ls
logrotate makewhatis.cron
[root@www.zhangfangzhou.cn cron.daily]# tail 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

4、Nginx日志轮询(转存)配置,Apache日志轮询(转存)类似。
/home/wwwlogs/ 网站日志目录

cat > /etc/logrotate.d/nginx << EOF
/home/wwwlogs/*log {
daily
rotate 5
missingok
dateext
compress
notifempty
sharedscripts
postrotate
    [ -e /usr/local/nginx/logs/nginx.pid ] && kill -USR1 \`cat /usr/local/nginx/logs/nginx.pid\`
endscript
}
EOF

5、为什么日志轮询(转存)生成日志的时间是凌晨三四点?
Logrotate是基于CRON运行的,所以这个时间是由CRON控制的,具体可以查询CRON的配置文件「/etc/crontab」
anacron 正在运行过去 crontab 未进行的各项工作排程

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 #只在03到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

每天都执行/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

6、立即执行logratate
ldconfig #执行
或者
/etc/cron.daily
logrotate
或者
执行下面
/usr/sbin/logrotate -f /etc/logrotate.d/nginx

由于logratate已经加到cron.daily(/etc/cron.daily/logrotate),不再需要加到计划任务中

7、logratate常用配置参数
/home/wwwlogs/*log:需要轮询日志路径
daily:每天轮询
rotate 5:保留最多5次滚动的日志
missingok:如果日志丢失,不报错继续滚动下一个日志
dateext:使用日期作为命名格式
compress:通过gzip压缩转储以后的日志
notifempty:当日志为空时不进行滚动
/usr/local/nginx/logs/nginx.pid: nginx pid位置,请查看nginx.conf
postrotate/endscript:在截断转储以后需要执行的命令

CentOS 7 可能出现如下问题
error: skipping "/home/wwwlogs/access_nginx.log" because parent directory has insecure permissions (It's world writable or writable by group which is not "root") Set "su" directive in config file to tell logrotate which user/group should be used for rotation.
解决方案
vi /etc/logrotate.d/nginx
su $http_user $http_group
su www www

转载至https://www.zhangfangzhou.cn/category/linux/centos/page/3#2

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值