常用日志维护脚本

日志格式

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

 

posted @ 2015-01-31 23:09  科学家会武术  阅读(571)  评论(0编辑  收藏  举报