首页 > 代码库 > 常用日志维护脚本
常用日志维护脚本
日志格式
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 started2015/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 started2015/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 started2015/01/04 13:22:05 [notice] 4644#0: signal process started2015/01/04 21:08:07 [notice] 7135#0: signal process started2015/01/11 20:59:24 [notice] 5201#0: signal process started2015/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.log2015/01/04 13:22:05 [notice] 4644#0: signal process started2015/01/04 21:08:07 [notice] 7135#0: signal process started2015/01/11 20:59:24 [notice] 5201#0: signal process started2015/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 started2015/01/04 21:08:07 [notice] 7135#0: signal process started2015/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 started2015/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 started2015/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 started2015/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 pathlogs_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").logkill -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
常用日志维护脚本
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。