wada811 / blog

wada811's blog
https://github.com/wada811/blog/issues
Apache License 2.0
6 stars 0 forks source link

grep --line-buffered でリアルタイムに tail を grep する #9

Open wada811 opened 5 years ago

wada811 commented 5 years ago

Why

tail でログを監視してみたが、関係ない情報が多いため grep で絞り込みたい。

What

tail -F access.log | grep "Status: 500" とすると grep がバッファリングしてしまうため、 行ごとにバッファリングしてリアルタイムに出力する。

How

grep--line-buffered オプションを使う。

上記の例では、以下のコマンドを実行する。

tail -F access.log | grep --line-buffered "Status: 500"

Ref

grep(1) BSD General Commands Manual

grep [--line-buffered] Force output to be line buffered. By default, output is line buffered when standard output is a terminal and block buffered otherwise.