bash Grep apache 服务器 500 错误到一个单独的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37382844/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Grep apache server 500 errors to a separate file
提问by tripleee
Hi I am trying to tail apache access logs and copy the errors to another file. I tried below options and all are working in command line but when triggered from a script they are not working.
嗨,我正在尝试跟踪 apache 访问日志并将错误复制到另一个文件。我尝试了以下选项,所有选项都在命令行中工作,但是当从脚本触发时,它们不起作用。
I understand the tail command is not exiting and so there is no output. But not sure how to overcome this.
我知道 tail 命令没有退出,所以没有输出。但不确定如何克服这一点。
/usr/bin/tail -f /apps/apache/logs/access_log | grep -h "HTTP\/1.1\" 50." >> /tmp/log_error_capture.txt
grep -m 1 "HTTP\/1.1\" 50." <(tail -f /apps/apache/logs/access_log)
( tail -f -n0 /apps/apache/logs/access_log & ) | grep -q "HTTP\/1.1\" 50." > /tmp/log_error_capture.txt
tail -f logfile |grep -m 1 "HTTP\/1.1\" 50." | xargs echo "" >> logfile \;
Can someone suggest a better way to grep the errors. Please.
有人可以建议更好的方法来grep错误。请。
回答by tripleee
If you want to monitor the growing log file, there is no way for your command to complete until the log file stops growing. I'm guessing you simply want to background the job:
如果要监视不断增长的日志文件,则在日志文件停止增长之前,您的命令无法完成。我猜你只是想为工作做背景:
tail -f access_log | grep "HTTP/1\.[01]\" 50." >> /tmp/log_error_capture.txt &
Incidentally, I removed the backslash before the slash (the slash is not a regex special, so it doesn't require escaping) and added one before the dot (where the opposite holds) and also updated the expression to accept HTTP/1.0 as well as 1.1. (I've occasionally seen 0.9, too, but I guess those are just testing and/or negligible anyway.) Also, because grep
is reading standard input, the -h
option was useless, so I removed it.
顺便说一句,我删除了斜杠之前的反斜杠(斜杠不是正则表达式特殊,所以它不需要转义)并在点之前添加一个(相反的地方),并且还更新了表达式以接受 HTTP/1.0 以及作为 1.1。(我也偶尔看到 0.9,但我想这些只是测试和/或可以忽略不计。)此外,因为grep
读取标准输入,该-h
选项没用,所以我删除了它。
This will continue to run, but not produce anything useful, when the log file is rotated. Perhaps you want to keep the PID and restart it as part of your log rotation script; or perhaps you simply want to run the grep
once at log rotation, instead of having it keep grinding in the background.
当日志文件轮换时,这将继续运行,但不会产生任何有用的信息。也许您想保留 PID 并重新启动它作为日志轮换脚本的一部分;或者您可能只是想grep
在日志轮换时运行一次,而不是让它在后台继续运行。
Alternatively, you could use tail --follow=name
to keep on running even across log rotations. For a production system, you should still figure out how to cope with log rotation for this script (i.e. how and when to rotate the file of 50x errors).
或者,您可以使用tail --follow=name
来继续运行,即使在日志轮换中也是如此。对于生产系统,您仍然应该弄清楚如何处理此脚本的日志轮换(即如何以及何时轮换 50x 错误的文件)。