SERVICE_RESULTに「start-limit-hit」が含まれるようにサービスを失敗させる方法

SERVICE_RESULTに「start-limit-hit」が含まれるようにサービスを失敗させる方法

そのため、ExecStopPostでスクリプトを実行するシステムサービスがあります。

[Unit]
Description=foobar-test
StartLimitBurst=3
StartLimitIntervalSec=120

[Service]
Type=simple
ExecStart=/do_something.sh
ExecStopPost=/handle_stop.sh $SERVICE_RESULT
Restart=on-failure
RestartSec=1

[Install]
...

"do_something.sh"スクリプトは戻りコード1で終了するため、開始制限に達するまでサービスが失敗しても再起動されます。

"handle_stop.sh"スクリプトは$ SERVICE_RESULTの内容のみを印刷します。

サービスが開始制限に達すると、$ SERVICE_RESULTの内容は期待どおりに「Start Limit Hit」の代わりに「Exit Code」です。文書

私が何か間違っているのか、それともシステムのバグですか?

systemd バージョン: systemd 244 (244.5+) +PAM -AUDIT -SELINUX +IMA -APPARMOR -SMACK +SYSVINIT +UTMP -LIBCRYPTSETUP -GCRYPT -GNUTLS +ACL +XZ -LZ4 -SECCOMP +BLKID -ELFUTILS +KMOD -IDN2 PCRE2基本階層=混合

ベストアンサー1

man 5 systemd.exec説明する:

"start-limit-hit": デバイスに対して開始制限が定義されており、制限に達するとデバイスは起動しません。

man 5 systemd.unit説明する:

開始制限に達したRestart =で構成されたデバイスは再起動を試みませんが、間隔が経過した後でも手動でまたはタイマーまたはソケットから再起動できます。

systemdこれは文書に従って行われたと主張することができます。start-limit-hitこれは、デバイスが起動に失敗した場合にのみ発生します。ただし、すでに起動しているデバイスをRestart=(再)起動しようとする試みは行われません。StartLimitBurst=StartLimitIntervalSec=

しかし、私はこのアプローチが現れるようにしました。

まず、バーストをトリガしてから別のバーストを試して手動systemctl startで起動しました。 systemdはその制限を超えるデバイスを再起動しないため、起動制限に達した後にデバイスが起動しないと想定します。真であれば、この実験で明示的に起動コマンドを発行し、必須コードを提供したときにデバイスは起動しません。

$ systemctl --user cat startlimit.service 
# ~/.config/systemd/user/startlimit.service
[Service]
ExecStart=/bin/false
Restart=always
StartLimitIntervalSec=20

$ systemctl --user start startlimit.service
$ systemctl --user start startlimit.service
Job for startlimit.service failed because the control process exited with error code.
See "systemctl --user status startlimit.service" and "journalctl --user -xeu startlimit.service" for details.

$ systemctl --user status startlimit.service
× startlimit.service
     Loaded: loaded (/home/stew/.config/systemd/user/startlimit.service; static)
     Active: failed (Result: exit-code) since Mon 2022-10-24 18:10:11 CEST; 5s ago
   Duration: 1ms
    Process: 5008 ExecStart=/bin/false (code=exited, status=1/FAILURE)
   Main PID: 5008 (code=exited, status=1/FAILURE)
        CPU: 1ms

Oct 24 18:10:11 systemd[1129]: startlimit.service: Scheduled restart job, restart counter is at 5.
Oct 24 18:10:11 systemd[1129]: Stopped startlimit.service.
Oct 24 18:10:11 systemd[1129]: startlimit.service: Start request repeated too quickly.
Oct 24 18:10:11 systemd[1129]: startlimit.service: Failed with result 'exit-code'.
Oct 24 18:10:11 systemd[1129]: Failed to start startlimit.service.
Oct 24 18:10:12 systemd[1129]: startlimit.service: Start request repeated too quickly.
Oct 24 18:10:12 systemd[1129]: startlimit.service: Failed with result 'exit-code'.
Oct 24 18:10:12 systemd[1129]: Failed to start startlimit.service.

この仮定は間違っていることが判明した。

次の推測は、「おそらくsystemctlがその制限を超えるデバイスの起動を拒否します」です。 「おそらく、これはシステムが自分で実行する必要があるかもしれません。」そのために、上記のような別のワンタイム仮想サービスを作成しましたWants=。それから何度も発動させました。

$ systemctl --user cat startlimit{,Wanter.service}
# ~/.config/systemd/user/startlimit.service
[Service]
ExecStart=/bin/false
Restart=always
StartLimitIntervalSec=20

[Install]
WantedBy=startlimitWanter.service

# ~/.config/systemd/user/startlimitWanter.service
[Service]
Type=oneshot
ExecStart=/bin/true

$ systemctl --user start startlimitWanter.service
$ systemctl --user start startlimitWanter.service
$ systemctl --user start startlimitWanter.service
$ systemctl --user status startlimit{,Wanter.service}
× startlimit.service
     Loaded: loaded (~/.config/systemd/user/startlimit.service; enabled; preset: enabled)
     Active: failed (Result: exit-code) since Mon 2022-10-24 18:13:49 CEST; 7s ago
   Duration: 1ms
    Process: 5066 ExecStart=/bin/false (code=exited, status=1/FAILURE)
   Main PID: 5066 (code=exited, status=1/FAILURE)
        CPU: 1ms


Oct 24 18:13:50 systemd[1129]: Failed to start startlimit.service.
Oct 24 18:13:52 systemd[1129]: startlimit.service: Start request repeated too quickly.
Oct 24 18:13:52 systemd[1129]: startlimit.service: Failed with result 'exit-code'.
Oct 24 18:13:52 systemd[1129]: Failed to start startlimit.service.

○ startlimitWanter.service
     Loaded: loaded (~/.config/systemd/user/startlimitWanter.service; static)
     Active: inactive (dead)

Oct 24 18:13:48 systemd[1129]: Starting startlimitWanter.service...
Oct 24 18:13:48 systemd[1129]: Finished startlimitWanter.service.
Oct 24 18:13:50 systemd[1129]: Starting startlimitWanter.service...
Oct 24 18:13:50 systemd[1129]: Finished startlimitWanter.service.
Oct 24 18:13:52 systemd[1129]: Starting startlimitWanter.service...
Oct 24 18:13:52 systemd[1129]: Finished startlimitWanter.service.

その人もあなたのニュースを聞くことができませんでした。

この時点で私はあなたがバグを見つけたことに同意したいと思います。

おすすめ記事