0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

ログ調査におけるtailコマンドについて

Posted at

先日ログの調査をするときにtailコマンドを使用した。
そもそもtailコマンドが何なのかわからなかったので、自分用に少し調べてまとめました。

tailコマンドとは?

テキストファイルの最後の数行を表示することができるコマンド。

tail ファイル名

で使用する。
ちなみにファイルの最初の数行を表示する場合は「head」を使う。

tailコマンドのオプション「-f」について

「-f」:ファイルの追記を監視する。

tail -f logfile

ログを流すときに、この-fオプションを使用した。
ファイルの追記を監視して、ファイルに追記があったらその部分を表示してくれる。
Ctrl+Cで終了できる。

grepでの抽出

本番環境で上記の「-f」コマンドでログを流すともの凄い勢いでログが流れるので追うことができない。
そんなときにgrepコマンドを使うと監視したいキーワードで抽出ができる。

tail -f logfile | grep "keyword"

こうすると"keyword"という文字列が含まれる行だけが出力されるようになる。
絞り込んだ内容にkeyword2が含まれる行を除外したい場合は以下のようにする。

tail -f logfile | grep "keyword" | grep -v "keyword2" 

また、grepを使用するとバッファリングしてしまうので、リアルタイムではなくなりバッファが一定量になるまで出力されない。
そんなときは、grepに--line-bufferedのオプションをつけることでバッファリングしなくなる。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?