随笔 - 121  文章 - 0  评论 - 35  阅读 - 85万

nginx切割日志

1、nginx不停服务进行切割日志:

1
2
3
4
5
6
7
8
9
10
11
12
[root@weblogic scripts]# cat nginx_log.sh
#!/bin/bash
 
log_path=/var/log/nginx
YESTERDAY=`date -d "yesterday" +%Y-%m-%d_%S`
nginx_cmd="/usr/sbin/nginx"
 
/bin/mv ${log_path}/access.log ${log_path}/access.$YESTERDAY.log
/bin/mv ${log_path}/error.log  ${log_path}/error.$YESTERDAY.log
 
# reopen a new log file
${nginx_cmd} -s reopen

 然后进行定时任务设定,设置在凌晨12点:

1
2
[root@weblogic nginx]# crontab -l
0 0 * * * /bin/bash /root/scripts/nginx_log.sh

 测试效果:

1
2
3
4
5
6
[root@weblogic nginx]# ll
总用量 8
-rw-r--r-- 1 nginx root   0 12月 15 02:13 access.2017-12-14_01.log
-rw-r--r-- 1 nginx root   0 12月 15 08:04 access.log
-rw-r--r-- 1 nginx root   0 12月 15 03:09 error.2017-12-14_01.log
-rw-r--r-- 1 nginx root  61 12月 15 08:04 error.log

 关于nginx命令中的几个信号处理,请查看官方文档:http://nginx.org/en/docs/beginners_guide.html

 

2、使用kill命令向nginx主进程发送一个信号:

#向nginx主进程发送USR1信号,重新打开日志文件,否则会继续往mv后的文件写数据的。原因在于:linux系统中,内核是根据文件描述符来找文件的。如果不这样操作导致日志切割失败。

1
# kill -USR1 `ps axu | grep "nginx: master process" | grep -v grep | awk '{print $2}'`

 所以该切割脚本如下:

1
2
3
4
5
6
7
8
9
10
11
12
[root@weblogic scripts]# cat nginx_sin.sh
#!/bin/bash
 
log_path=/var/log/nginx
YESTERDAY=`date -d "yesterday" +%Y-%m-%d_%S`
nginx_cmd="/usr/sbin/nginx"
 
/bin/mv ${log_path}/access.log ${log_path}/access.$YESTERDAY.log
/bin/mv ${log_path}/error.log  ${log_path}/error.$YESTERDAY.log
 
# send a signal
/bin/kill -USR1 `ps axu | grep "nginx: master process" | grep -v grep | awk '{print $2}'`

 执行结果如下:

1
2
3
4
5
6
[root@weblogic nginx]# ll
总用量 8
-rw-r--r-- 1 nginx root   0 12月 15 08:04 access.2017-12-14_49.log
-rw-r--r-- 1 nginx root   0 12月 15 08:28 access.log
-rw-r--r-- 1 nginx root  61 12月 15 08:04 error.2017-12-14_49.log
-rw-r--r-- 1 nginx root   0 12月 15 08:28 error.log

 现在将脚本放进到crontab定时脚本中,凌晨12点执行:

1
2
[root@weblogic nginx]# crontab -l
0 0 * * * /bin/bash /root/scripts/nginx_sin.sh

 

3、使用logrotate进行切割:

 1、安装:

yum -y install logrotate

2、配置logrotate:

复制代码
[root@weblogic logrotate.d]# pwd
/etc/logrotate.d
[root@weblogic logrotate.d]# cat nginx
/var/log/nginx/*log {
    daily
    rotate 10
    dateext
    missingok
    notifempty
    # compress
    delaycompress
    create 640 nginx adm
    sharedscripts
    postrotate
        [ -f /var/run/nginx.pid ] && /bin/kill -USR1 $(cat /var/run/nginx.pid 2>/dev/null) 2>/dev/null || :
    endscript
}
复制代码

3、配置参数:

/var/log/nginx/为nginx日志的存储目录,可以根据实际情况进行修改。

daily:日志文件将按天轮循。

weekly:日志文件将按周轮循。

monthly:日志文件将按月轮循。

missingok:在日志轮循期间,任何错误将被忽略,例如“文件无法找到”之类的错误。

rotate 7:一次存储7个日志文件。对于第8个日志文件,时间最久的那个日志文件将被删除。

dateext:定义日志文件后缀是日期格式,也就是切割后文件是:xxx.log-20160402.gz这样的格式。如果该参数被注释掉,切割出来是按数字递增,即前面说的 xxx.log-1这种格式。

compress:在轮循任务完成后,已轮循的归档将使用gzip进行压缩。

delaycompress:总是与compress选项一起用,delaycompress选项指示logrotate不要将最近的归档压缩,压缩将在下一次轮循周期进行。这在你或任何软件仍然需要读取最新归档时很有用。

notifempty:如果是空文件的话,不进行转储。

create 640 nginx adm:以指定的权限和用书属性,创建全新的日志文件,同时logrotate也会重命名原始日志文件。

postrotate/endscript:在所有其它指令完成后,postrotate和endscript里面指定的命令将被执行。在这种情况下,rsyslogd进程将立即再次读取其配置并继续运行。注意:这两个关键字必须单独成行。

4、查看logrotate切割日志的时间:

复制代码
[root@weblogic logrotate.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
复制代码
# the jobs will be started during the following hours only
START_HOURS_RANGE=3-22
posted on   wadeson  阅读(3099)  评论(0)    收藏  举报
编辑推荐:
· C#高性能开发之类型系统:从 C# 7.0 到 C# 14 的类型系统演进全景
· 从零实现富文本编辑器#3-基于Delta的线性数据结构模型
· 记一次 .NET某旅行社酒店管理系统 卡死分析
· 长文讲解 MCP 和案例实战
· Hangfire Redis 实现秒级定时任务,使用 CQRS 实现动态执行代码
阅读排行:
· C#高性能开发之类型系统:从 C# 7.0 到 C# 14 的类型系统演进全景
· 管理100个小程序-很难吗
· 在SqlSugar的开发框架中增加对低代码EAV模型(实体-属性-值)的WebAPI实现支持
· 如何统计不同电话号码的个数?—位图法
· 使用这个工具,基于代码仓库直接生成教程文档,感觉比我自己写的还好
< 2025年4月 >
30 31 1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 1 2 3
4 5 6 7 8 9 10

点击右上角即可分享
微信分享提示