ログの出力結果をファイルに保存する

LINEで送る
Pocket

ログの在処は分かったけれど、ログファイルの参照はどうすればよいだろうか?もちろん好きなエディタを開いて、問題のある箇所を探せばよいがtailfコマンドを使えばファイルの末端10行(-n オプションで行数指定可能)が表示され、その後ファイルに新規に追加された行を表示し続けてくれるのでログをリアルタイムに監視したい場合、有効です。

下記は、tailf コマンドを実行しながら httpd サービスの再起動を行ってみたものです。

$ sudo systemctl restart httpd.service
$ sudo tailf /var/log/messages
Apr  2 11:10:39 localhost systemd: Stopping The Apache HTTP Server...
Apr  2 11:10:40 localhost systemd: Starting The Apache HTTP Server...
Apr  2 11:10:40 localhost httpd: AH00526: Syntax error on line 42 of /etc/httpd/conf/httpd.conf:          <- httpd.conf の 42 行目にエラーがある事が分かった。
Apr  2 11:10:40 localhost httpd: Invalid address or port
Apr  2 11:10:40 localhost systemd: httpd.service: main process exited, code=exited, status=1/FAILURE
Apr  2 11:10:40 localhost kill: kill: cannot find process ""
Apr  2 11:10:40 localhost systemd: httpd.service: control process exited, code=exited status=1
Apr  2 11:10:40 localhost systemd: Failed to start The Apache HTTP Server.
Apr  2 11:10:40 localhost systemd: Unit httpd.service entered failed state.
Apr  2 11:10:40 localhost systemd: httpd.service failed.

このように、リアルタイムでログを監視出来るので、どんな操作をしたらどんな問題が発生するのかをウォッチする事ができる。使用しているUSBメモリに問題があると感じた場合は、tailf コマンドでログをウォッチしならがUSBの抜き差し、書き込みなどを行えばどんな問題が発生しているかウォッチ出来るだろう。

また、tailf コマンドの反対のコマンドとして head というコマンドがある。これは、ファイルの先頭10行(-n オプションで行数指定可能)が表示されるコマンドです。システム起動時、サービス開始時の問題を発見するのに便利なコマンドです。

$ sudo head /var/log/messages
LINEで送る
Pocket

  • このエントリーをはてなブックマークに追加

コメントを残す

*

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください