码迷,mamicode.com
首页 > 其他好文 > 详细

常用日志维护脚本

时间:2015-02-01 00:33:01      阅读:653      评论:0      收藏:0      [点我收藏+]

标签:

日志格式

2015/01/03 22:13:53 [error] 30310#0: open() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or directory)
2015/01/03 22:15:54 [notice] 31090#0: signal process started
2015/01/03 22:15:54 [error] 31090#0: open() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or directory)
2015/01/03 22:19:51 [notice] 32601#0: signal process started
2015/01/03 22:19:51 [error] 32601#0: open() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or directory)
2015/01/03 23:01:51 [notice] 4960#0: signal process started
2015/01/04 13:22:05 [notice] 4644#0: signal process started
2015/01/04 21:08:07 [notice] 7135#0: signal process started
2015/01/11 20:59:24 [notice] 5201#0: signal process started
2015/01/12 23:31:29 [notice] 5121#0: signal process started

1. 打印大于2015年1月3号的日志

shell> gawk -F   {if($1>"2015/01/03"){print $0}} /usr/local/nginx/logs/error.log

2015/01/04 13:22:05 [notice] 4644#0: signal process started
2015/01/04 21:08:07 [notice] 7135#0: signal process started
2015/01/11 20:59:24 [notice] 5201#0: signal process started
2015/01/12 23:31:29 [notice] 5121#0: signal process started

2. 打印1月3号到1月12号之间的日志

shell> gawk -F   {if($1>"2015/01/03"&&$1<"2015/01/12"){print $0}} /usr/local/nginx/logs/error.log 

2015/01/04 13:22:05 [notice] 4644#0: signal process started
2015/01/04 21:08:07 [notice] 7135#0: signal process started
2015/01/11 20:59:24 [notice] 5201#0: signal process started

3. 打印当天22:13:53以后的日志

shell> gawk -F   {if($1=="2015/01/03"&&($2>"22:13:53")){print $0}} /usr/local/nginx/logs/error.log 

2015/01/03 22:15:54 [notice] 31090#0: signal process started
2015/01/03 22:15:54 [error] 31090#0: open() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or directory)
2015/01/03 22:19:51 [notice] 32601#0: signal process started
2015/01/03 22:19:51 [error] 32601#0: open() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or directory)
2015/01/03 23:01:51 [notice] 4960#0: signal process started

4. 打印5分钟内的日志

shell> gawk -F   {if($1=="2015/01/03"&&($2>"22:13:53"&&$2<"22:18:53")){print $0}} /usr/local/nginx/logs/error.log 
2015/01/03 22:15:54 [notice] 31090#0: signal process started
2015/01/03 22:15:54 [error] 31090#0: open() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or directory)

5. 每天定时切割nginx日志脚本

#!/bin/bash
# This script run at 00:00

# The Nginx logs path
logs_path="/usr/local/webserver/nginx/logs/"

mkdir -p ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/
mv ${logs_path}access.log ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/access_$(date -d "yesterday" +"%Y%m%d").log
kill -USR1 `cat /usr/local/webserver/nginx/nginx.pid`

设置crontab,每天凌晨00:00切割nginx访问日志
crontab -e

输入以下内容:
引用
00 00 * * * /bin/bash  /usr/local/webserver/nginx/sbin/cut_nginx_log.sh

 

常用日志维护脚本

标签:

原文地址:http://www.cnblogs.com/phpfans/p/4264895.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!