【Linux】【CentOS7】journalctl コマンド

CentOS 7 より追加された非常に使い勝手が良い journalctl コマンドについて解説します。

OSを管理する際にサービス周りの調査でよく利用します。

 

 

journalctl コマンド

journalctl コマンドについての「解説」「使い方」について解説します。

 

journalctlコマンドの解説

man コマンドでの「journalctl」コマンドの解説部分の抜粋です。

DESCRIPTION
       journalctl may be used to query the contents of the systemd(1) journal as written by systemd-journald.service(8).

    journalctl コマンドは、systemd-journald.service(8)によって書かれた systemd(1)ジャーナルの内容を照会するために使用されます。
    → ジャーナルとは「通信記録」とか「更新履歴」のことを言います。

       If called without parameters, it will show the full contents of the journal, starting with the oldest entry collected.

    パラメータなしでコマンドを実行すると、収集された最も古いエントリから開始して、ジャーナルの全内容が表示されます。

       If one or more match arguments are passed, the output is filtered accordingly. 

       1つ以上の一致引数が渡された場合、出力はそれに応じてフィルタリングされます。

 

     A match is in the format “FIELD=VALUE”, e.g.  “_SYSTEMD_UNIT=httpd.service”, referring to the components of a structured journal entry. 

    一致させる(マッチさせる)ための構文は「FIELD = VALUE」です。【例】 “_SYSTEMD_UNIT = httpd.service” は、構造化ジャーナルエントリのコンポーネントを参照します。

 

    See systemd.journal-fields(7) for a list of well-known fields.

    よく知られているフィールドのリストについては、systemd.journal-fields(7)を参照してください。

 

       If multiple matches are specified matching different fields, the log entries are filtered by both, i.e. the resulting output will show only entries matching all the specified matches of this kind. 

    異なるフィールドに一致(マッチ)する複数の一致(マッチ)が指定された場合、ログエントリは両方によってフィルタリングされます。つまり、結果の出力には、この種の指定されたすべての一致に一致するエントリのみが表示されます。

 

       If two matches apply to the same field, then they are automatically matched as alternatives, i.e. the resulting output will show entries matching any of the specified matches for the same field. 

    もし同じフィールドに2つの一致「マッチ」があった場合、それらは自動的に代替として一致します。つまり、結果の出力には、同じフィールドの指定された一致のいずれかに一致するエントリが表示されます。

 

    Finally, the character “+” may appears as a separate word between other terms on the command line. 

    最後に、文字「+」は、コマンドライン上の他の単語の間に別の単語として表示されることがあります。

 

       This causes all matches before and after to be combined in a disjunction (i.e. logical OR).

     これにより、前と後のすべてのマッチが論理和で結合されます(論理OR)。

       As shortcuts for a few types of field/value matches, file paths may be specified. 

    いくつかのタイプのフィールド/値の一致のショートカットとして、ファイルパスを指定することができます。

 

       If a file path refers to an executable file, this is equivalent to an “_EXE=” match for the canonicalized binary path. 

     ファイルパスが実行可能ファイルを参照する場合、これは正規化されたバイナリパスの “_EXE =”に相当します。

 

       Similarly, if a path refers to a device node then match is added for the kernel name of the device (“_KERNEL_DEVICE=”). Also, matches for the kernel names of all the parent devices are added automatically. 

    同様に、パスがデバイスノードを参照する場合、デバイスのカーネル名( “_KERNEL_DEVICE =”)に一致が追加されます。また、すべての親デバイスのカーネル名の一致が自動的に追加されます。

 

       Device node paths are not stable across reboots, therefore match for the current boot id (“_BOOT_ID=”) is always added as well. 

    デバイスノードのパスはリブートしても安定しないため、現在のブートID( “_BOOT_ID =”)とのマッチングも常に追加されます。

 

       Note that only the log entries for the existing device nodes maybe queried by providing path to the device node.

    既存のデバイスノードのログエントリだけが、デバイスノードへのパスを提供することによって照会され得ることに留意してください。

       Additional constraints may be added using options –boot, –unit=, etc, to further limit what entries will be shown (logical AND).

    追加の制約は、 – boot、–unit =などのオプションを使用して追加して、どのエントリが表示されるかをさらに制限できます(論理AND)。

       Output is interleaved from all accessible journal files, whether they are rotated or currently being written, and regardless of whether they belong to the system itself or are accessible user journals.

    出力は、回転しているか書き込み中であっても、システム自体に属しているかアクセス可能なユーザージャーナルであるかにかかわらず、アクセス可能なすべてのジャーナルファイルからインターリーブされます。

       The set of journal files which will be used can be modified using the –user, –system, –directory, and –file options, see below.

    使用されるジャーナルファイルのセットは、–user、–system、 – directory、および – fileオプションを使用して変更できます。以下を参照してください。

 

       All users are granted access to their private per-user journals. 

    すべてのユーザーには、ユーザーごとの個人専用のジャーナルへのアクセス権が与えられます。

 

    However, by default, only root and users who are members of a few special groups are granted access to the system journal and the journals of other users. 

    ただし、デフォルトでは、ルートと特定の少数のグループのメンバーであるユーザーのみが、システムジャーナルと他のユーザーのジャーナルにアクセスできます。

 

    Members of the the “systemd-journal”, “adm”, and “wheel” groups can read all journal files. 

    「systemd-journal」、「adm」、および「wheel」グループのメンバーは、すべてのジャーナルファイルを読み取ることができます。

 

       Note that the two latter groups traditionally have additional privileges specified by the distribution. 

    後者の2つのグループには伝統的に、配布によって指定された追加の特権があることに注意してください。

 

       Members of the “wheel” group can often perform administrative tasks.

    「wheel」グループのメンバーは、しばしば管理タスクを実行できます。

 

       The output is paged through less by default, and long lines are “truncated” to screen width. 

      出力は、既定では less コマンドによってページングされ、長い行は画面の幅に対して「切り捨てられます」。

 

       The hidden part can be viewed by using the left-arrow and right-arrow keys. 

    出力は、既定ではlessによってページされ、長い行は画面の幅に対して「切り捨てられます」。

 

       Paging can be disabled; see the –no-pager option and the “Environment” section below.

    ページングは​​無効にすることができます。以下の–no-pagerオプションと “Environment”セクションを参照してください。

 

       When outputting to a tty, lines are colored according to priority: lines of level ERROR and higher are colored red; lines of level NOTICE and higher are highlighted; other lines are displayed normally.

    ttyに出力するとき、行は優先度に応じて色付けされます。レベルERROR以上の行は赤色に着色され、レベルNOTICE以上の行が強調表示されます。他の行は正常に表示されます。

 

 

journalctl コマンドのみ

引数(オプション)を何もつけずに「journalctl」コマンドだけを実行すると一覧古い情報からジャーナルを表示します。

[root@SAKURA_VPS /]# journalctl

— Logs begin at 月 2018-03-12 00:00:01 JST, end at 土 2018-03-17 14:20:01 JST. —
 3月 12 00:00:01 txx-xxx-xxxxx.vs.sakura.ne.jp CROND[10340]: (root) CMD (/usr/lib64/sa/sa1 1 1)
 3月 12 00:00:01 txx-xxx-xxxxx.vs.sakura.ne.jp rsyslogd[590]: imjournal: journal reloaded… [v8.24.0 try http://www.rsyslog.com/e/0 ]
 3月 12 00:00:01 txx-xxx-xxxxx.vs.sakura.ne.jp systemd[1]: Removed slice User Slice of root.
 3月 12 00:00:01 txx-xxx-xxxxx.vs.sakura.ne.jp systemd[1]: Stopping User Slice of root.
 3月 12 00:10:01 txx-xxx-xxxxx.vs.sakura.ne.jp systemd[1]: Created slice User Slice of root.
 3月 12 00:10:01 txx-xxx-xxxxx.vs.sakura.ne.jp systemd[1]: Starting User Slice of root.
 3月 12 00:10:01 txx-xxx-xxxxx.vs.sakura.ne.jp systemd[1]: Started Session 19433 of user root.
 3月 12 00:10:01 txx-xxx-xxxxx.vs.sakura.ne.jp systemd[1]: Starting Session 19433 of user root.
 3月 12 00:10:01 txx-xxx-xxxxx.vs.sakura.ne.jp systemd[1]: Started Session 19432 of user root.
 3月 12 00:10:01 txx-xxx-xxxxx.vs.sakura.ne.jp systemd[1]: Starting Session 19432 of user root.
 3月 12 00:10:01 txx-xxx-xxxxx.vs.sakura.ne.jp CROND[10915]: (root) CMD (rm -f /var/log/python/app.log)
 3月 12 00:10:01 txx-xxx-xxxxx.vs.sakura.ne.jp CROND[10916]: (root) CMD (/usr/lib64/sa/sa1 1 1)
 3月 12 00:10:01 txx-xxx-xxxxx.vs.sakura.ne.jp systemd[1]: Removed slice User Slice of root.
 3月 12 00:10:01 txx-xxx-xxxxx.vs.sakura.ne.jp systemd[1]: Stopping User Slice of root.

 

ジャーナルログは、「systemd」だけでなく「CROND」や「sshd」のログも表示されます。

 

 

各種サービスが起動しない時に調査するために使う

httpd や MySQL や PostgreSQL など各種サービスが起動しない時に、原因調査のために「journal」コマンドを利用します。

 

【例】PostgreSQL が起動しない場合

[root@SAKURA_VPS ~]# systemctl -l start postgresql
Job for postgresql.service failed because the control process exited with error code. See “systemctl status postgresql.service” and “journalctl -xe” for details.
[root@SAKURA_VPS ~]#

 

上記は PostgreSQL サービスが起動しない時に出力されたメッセージです。

「journalctl -xe」コマンドを実行してくださいとメッセージが出ます。

 

journalctl -xe コマンドを実行します。

詳細が表示されますが、右端でメッセージが切れてしまうため最後までメッセージが読めません。

[root@SAKURA_VPS ~]# journalctl -xe

 3月 18 08:02:51 txx-xxx-xxxxx.vs.sakura.ne.jp systemd[1]: Starting PostgreSQL database server…
— Subject: Unit postgresql.service has begun start-up
— Defined-By: systemd
— Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel

— Unit postgresql.service has begun starting up.
 3月 18 08:02:51 txx-xxx-xxxxx.vs.sakura.ne.jp postgresql-check-db-dir[2276]: “/var/lib/pgsql/data” is m
 3月 18 08:02:51 txx-xxx-xxxxx.vs.sakura.ne.jp postgresql-check-db-dir[2276]: Use “postgresql-setup init
 3月 18 08:02:51 txx-xxx-xxxxx.vs.sakura.ne.jp postgresql-check-db-dir[2276]: See /usr/share/doc/postgre
 3月 18 08:02:51 txx-xxx-xxxxx.vs.sakura.ne.jp systemd[1]: postgresql.service: control process exited, c
 3月 18 08:02:51 txx-xxx-xxxxx.vs.sakura.ne.jp systemd[1]: Failed to start PostgreSQL database server.
— Subject: Unit postgresql.service has failed
— Defined-By: systemd
— Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel

— Unit postgresql.service has failed.

— The result is failed.
 3月 18 08:02:51 txx-xxx-xxxxx.vs.sakura.ne.jp systemd[1]: Unit postgresql.service entered failed state.
 3月 18 08:02:51 txx-xxx-xxxxx.vs.sakura.ne.jp systemd[1]: postgresql.service failed.
 3月 18 08:02:51 txx-xxx-xxxxx.vs.sakura.ne.jp polkitd[544]: Unregistered Authentication Agent for unix-

 

メッセージが右端で切れている場合は「→」(右矢印キー)で表示させることができる

メッセージが右端で切れる場合は「→」キーで右端を表示させることができます。

しかし、下図でも分かりますが右側を表示させることはできても見にくいです。

やはり全体をパッと一目で見えるようにしたいです。

 

 

journalctl 表示形式のオプション

「journal -xe」コマンドではページャー(pager)が「less」コマンドになり、メッセージの右端が省略されます。

 

journalctl のオプションを確認します。

       The output is paged through less by default, and long lines are “truncated” to screen width.
       The hidden part can be viewed by using the left-arrow and right-arrow keys. Paging can be
       disabled; see the –no-pager option and the “Environment” section below. ← 長いメッセージは枠に収まるようにトランケート(削除)されるから「←」キーや「→」キーで隠れているメッセージを見ることができると言っています。また、「–no-pager」オプションもあるから「Environment」セクションも見るように言っています。

 

 

OPTIONS
       The following options are understood:

       -e, –pager-end
           Immediately jump to the end of the journal inside the implied pager tool. This implies
           -n1000 to guarantee that the pager will not buffer logs of unbounded size. This may be
           overridden with an explicit -n with some other numeric value while -nall will disable this
           cap. Note that this option is only supported for the less(1) pager.

      ← -e オプションを付けると最後の行を表示します。

       -x, –catalog
           Augment log lines with explanation texts from the message catalog. 

           ログ・ラインをメッセージ・カタログの説明テキストで拡張します。
           This will add explanatory help texts to log messages in the output where this is available. 

           これは、利用可能な出力のログメッセージに説明的なヘルプテキストを追加します。

           These short help texts will explain the context of an error or log event, possible solutions, as well as pointers to support forums, developer documentation, and any other relevant manuals.

           これらの短いヘルプテキストでは、エラーまたはログイベントのコンテキスト、解決策、サポートフォーラムへのポインタ、開発者向けドキュメント、その他の関連マニュアルについて説明します。
           Note that help texts are not available for all messages, but only for selected ones. 

           ヘルプテキストは、すべてのメッセージで利用できるわけではなく、選択したものについてのみ利用可能であることに注意してください。

           For more information on the message catalog, please refer to the Message Catalog Developer
           Documentation[4].
           メッセージカタログの詳細については、メッセージカタログ開発者ドキュメント[4]を参照してください。

           Note: when attaching journalctl output to bug reports, please do not use -x.

           注意:journalctlの出力をバグレポートに添付するときは、-xを使用しないでください。

 

ENVIRONMENT
       $SYSTEMD_PAGER
           Pager to use when –no-pager is not given; overrides $PAGER. Setting this to an empty
           string or the value “cat” is equivalent to passing –no-pager.

           → –no-pager オプションでページャー(less)を解除できます。環境変数 $PAGER が空の時は「cat」で表示されます。

 

 

 

「–no-pager」オプションを付けて「journalctl -x –no-pager」コマンドを実行してみます。

[root@SAKURA_VPS ~]# journalctl -x –no-pager

 

~ 省略 ~

 

 3月 18 08:02:51 txx-xxx-xxxxx.vs.sakura.ne.jp systemd[1]: Starting PostgreSQL database server…
— Subject: Unit postgresql.service has begun start-up
— Defined-By: systemd
— Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel

— Unit postgresql.service has begun starting up.
 3月 18 08:02:51 txx-xxx-xxxxx.vs.sakura.ne.jp postgresql-check-db-dir[2276]: “/var/lib/pgsql/data” is missing or empty.
 3月 18 08:02:51 txx-xxx-xxxxx.vs.sakura.ne.jp postgresql-check-db-dir[2276]: Use “postgresql-setup initdb” to initialize the database cluster.
 3月 18 08:02:51 txx-xxx-xxxxx.vs.sakura.ne.jp postgresql-check-db-dir[2276]: See /usr/share/doc/postgresql-9.2.23/README.rpm-dist for more information.
 3月 18 08:02:51 txx-xxx-xxxxx.vs.sakura.ne.jp systemd[1]: postgresql.service: control process exited, code=exited status=1
 3月 18 08:02:51 txx-xxx-xxxxx.vs.sakura.ne.jp systemd[1]: Failed to start PostgreSQL database server.
— Subject: Unit postgresql.service has failed
— Defined-By: systemd
— Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel

— Unit postgresql.service has failed.

— The result is failed.
 3月 18 08:02:51 txx-xxx-xxxxx.vs.sakura.ne.jp systemd[1]: Unit postgresql.service entered failed state.
 3月 18 08:02:51 txx-xxx-xxxxx.vs.sakura.ne.jp systemd[1]: postgresql.service failed.
 3月 18 08:02:51 txx-xxx-xxxxx.vs.sakura.ne.jp polkitd[544]: Unregistered Authentication Agent for unix-process:2269:590266406 (system bus name :1.42943, object path /org/freedesktop/PolicyKit1/AuthenticationAgent, locale ja_JP.UTF-8) (disconnected from bus)

 

しかし「–no-pager」オプションを付けると「cat」コマンドになるのでログの量が長い時はひたすらログが出力され終わるのを待ち続けるしかありません。

 

一番使い勝手がいいのは「–no-pager」オプションで「less」コマンド

「–no-pager」オプションを付けると「cat」コマンドになるので、それをパイプでつないで「less」コマンドで表示させると、メッセージの右端はトランケートされないし、見やすくなります。

さらに「less」コマンドなので「検索」「目的の行への移動」もできます。

[root@SAKURA_VPS ~]# journalctl -x –no-pager | less  

 

3月 18 08:02:51 txx-xxx-xxxxx.vs.sakura.ne.jp systemd[1]: Starting PostgreSQL database
 server…
— Subject: Unit postgresql.service has begun start-up
— Defined-By: systemd
— Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel

— Unit postgresql.service has begun starting up.
 3月 18 08:02:51 txx-xxx-xxxxx.vs.sakura.ne.jp postgresql-check-db-dir[2276]: “/var/lib/pgsql/data” is missing or empty.
 3月 18 08:02:51 txx-xxx-xxxxx.vs.sakura.ne.jp postgresql-check-db-dir[2276]: Use “postgresql-setup initdb” to initialize the database cluster.
 3月 18 08:02:51 txx-xxx-xxxxx.vs.sakura.ne.jp postgresql-check-db-dir[2276]: See /usr/share/doc/postgresql-9.2.23/README.rpm-dist for more information.
 3月 18 08:02:51 txx-xxx-xxxxx.vs.sakura.ne.jp systemd[1]: postgresql.service: controlprocess exited, code=exited status=1
 3月 18 08:02:51 txx-xxx-xxxxx.vs.sakura.ne.jp systemd[1]: Failed to start PostgreSQL database server.

 

 

 

 

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

コメント

コメントする

AlphaOmega Captcha Medica  –  What Do You See?
     
 

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