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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!