テンプレートsystemctlが停止していることを確認する方法

テンプレートsystemctlが停止していることを確認する方法

私の問題は次のようなものだと思います。

https://askubuntu.com/questions/941775/stopped-custom-services-control-by-systemd-do-not-appear-in-systemctl-list-un

しかし、役に立つ答えが得られず、もう一度やり直しました!

私にとって必要なのは、停止したテンプレートサービスが開始されたことがあるかどうかを知ることです。

これは私のサービスです。

$ cat /etc/systemd/system/[email protected] 
[Unit]
Description=Systemctl Example
StartLimitIntervalSec=0
StartLimitBurst=0

[Service]
ExecStart=/bin/echo %i >> /opt/template.log
Restart=always
RestartSec=1s

[Install]
WantedBy=multi-user.target

サービスインスタンスを起動して停止すると、そのインスタンスがリスト単位リスト:

$ sudo systemctl start example@100
$ sudo systemctl start example@200
$ sudo systemctl stop example@200
$ systemctl list-units --full --all --plain --no-legend --no-pager|grep example
[email protected]                                                                                                loaded    activating auto-restart Systemctl Example
system-example.slice                                                                                               loaded    active     active       Slice /system/example
$ service example@100 status[email protected] - Systemctl Example
     Loaded: loaded (/etc/systemd/system/example@ [...]
     Active: activating (auto-restart) since Wed 2022-11-02 11:20:54 CET; 799ms ago
    Process: 41042 ExecStart=/bin/echo 100 >> /opt/template.log (code=exited, status=0/SUCCESS)
   Main PID: 4102 (code=exited, status=0/SUCCESS)
        CPU: 1ms
nov. 02 11:20:54 systemd[1]: Started Systemctl Example.
nov. 02 11:20:55 echo[41042]: 100 >> /opt/template.log
nov. 02 11:20:54 systemd[1]: [email protected]: Deactivated successfully.

「非テンプレート」サービスと同じ動作が予想されます。

$ sudo systemctl stop bluetooth
$ systemctl list-units --full --all --plain --no-legend --no-pager|grep bluetooth
bluetooth.service                                                                                               loaded    inactive   dead         Bluetooth service
bluetooth.target                                                                                                loaded    active     active       Bluetooth Support

これ状態サービスがテンプレートであるかどうかに関するオプションは非常に異なります。

$ systemctl status bluetooth
○ bluetooth.service - Bluetooth service
     Loaded: loaded (/lib/systemd/system/bluetooth.service; enabled; vendor preset: enabled)
     Active: inactive (dead) since Mon 2022-10-31 11:47:25 CET; 2min 18s ago
       Docs: man:bluetoothd(8)
    Process: 1071 ExecStart=/usr/lib/bluetooth/bluetoothd (code=exited, status=0/SUCCESS)
   Main PID: 1071 (code=exited, status=0/SUCCESS)
     Status: "Powering down"
        CPU: 56ms

$ systemctl status example@200 # this service instance has just been stopped[email protected] - Systemctl Example
     Loaded: loaded (/etc/systemd/system/[email protected]; disabled; vendor preset: enabled)
     Active: inactive (dead)

$ systemctl status toto@2000 # this service instance has never been started[email protected] - Systemctl Example
     Loaded: loaded (/etc/systemd/system/[email protected]; disabled; vendor preset: enabled)
     Active: inactive (dead)

テンプレートサービスに「xxxxxxから; Y分前」が欠落しているのはなぜですか?

Ubuntu 22.04を使用していますが、Ubuntu 18.04でも同じ問題があります。

$ systemctl --version
systemd 249 (249.11-0ubuntu3.6)

ベストアンサー1

これは次の点に役立ちます。

sudo journalctl -xe -u example@200

これにより、デバイスのログが表示されます(開始、終了、停止などのイベントはこのコマンドで処理されます)。

たとえば、私の場合は、デバイスを停止し[email protected]て実行すると、次のような出力が得られました。sudo journalctl -xe -u [email protected]

Oct 31 20:53:11 localhost.localdomain systemd[1]: Stopping Getty on tty1...
░░ Subject: A stop job for unit [email protected] has begun execution
░░ Defined-By: systemd
░░ Support: https://lists.freedesktop.org/mailman/listinfo/systemd-devel
░░ 
░░ A stop job for unit [email protected] has begun execution.
░░ 
░░ The job identifier is 13406.
Oct 31 20:53:11 localhost.localdomain systemd[1]: [email protected]: Deactivated successfully.
░░ Subject: Unit succeeded
░░ Defined-By: systemd
░░ Support: https://lists.freedesktop.org/mailman/listinfo/systemd-devel
░░ 
░░ The unit [email protected] has successfully entered the 'dead' state.
Oct 31 20:53:11 localhost.localdomain systemd[1]: Stopped Getty on tty1.
░░ Subject: A stop job for unit [email protected] has finished
░░ Defined-By: systemd
░░ Support: https://lists.freedesktop.org/mailman/listinfo/systemd-devel
░░ 
░░ A stop job for unit [email protected] has finished.
░░ 
░░ The job identifier is 13406 and the job result is done

したがって、grep上記の出力に基づいてデバイスが停止していることを確認できます。ただし、日付と時刻に基づいてgrepする必要があるかもしれません。

場合によっては、ユニット自体が正常に完了した可能性があります。したがって、次のメッセージを表示しないでくださいStoppedStoppingそれ以外の場合、Deactivated出力は次のようになりますFinished X unit... Subject: A start job for unit UNIT has finished successfully

ヒント1
特定の日付/時刻から特定の日付/時刻へのJournalctl出力を取得するには、引数を使用できます--since--until出力をgrepするには、そのオプションを使用できます--grep(これにより、このようなパイプの使用を避けることができますjournalctl ... | grep something)。 ~によるとman journalctl

-S, --以降=, -U, --until=

それぞれ指定された日付以降に項目の表示を開始するか、指定された日付より前に項目の表示を開始します。日付指定は「2012-10-30 18:17:16」の形式でなければなりません。時間部分を省略すると、「00:00:00」とみなされます。秒部分だけを省略すると、「:00」とみなされます。 ...

-g, --grep=

MESSAGE =フィールドが指定された正規表現に一致する項目で出力をフィルタリングします。 PERL互換正規表現の使用

したがって、次のようなものを使用して特定の項目を取得できます。

sudo journalctl -xe -u [email protected] --since='2022-11-02 12:14'  --grep='Stopped'

ヒント2:
使用する場合ユーザー単位(〜/.config/systemd/user/にあります)以下を使用する必要がありますjournalctl -xe --user -u example@200。 sudoを使用する場合ユーザー単位そのコマンドでログを表示できない場合があります(sudoを使用してログを表示するように設定できるかどうかはわかりません)。

おすすめ記事