再起動時にsystemdがサービスを停止するのを防ぐ方法

再起動時にsystemdがサービスを停止するのを防ぐ方法

外部デバイスのオン/オフを切り替える次のサービスファイルがあります。

[Unit]
Description=Manage the UR controller, turn it off when the system goes down.
Requires=network.target
After=network.target

[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/usr/local/sbin/ur_boot
ExecStop=/usr/local/sbin/ur_shutdown

[Install]
WantedBy=multi-user.target

うまくいきますが、外部デバイスの起動とシャットダウンに時間がかかるため、再起動すると再準備が完了するまで時間がかかります。

再起動時にサービスを停止せずにスケジュールされたシャットダウンを停止するようにsystemdに指示できますか?

ベストアンサー1

この回答はコメントが長すぎます。しかし、それがあなたに良い方向を提示することを願っています。

からman systemd.special

   shutdown.target
       A special target unit that terminates the services on system shutdown.

       Services that shall be terminated on system shutdown shall add Conflicts= 
       and Before= dependencies to this unit for their service unit, which is 
       implicitly done when DefaultDependencies=yes is set (the default).

すべてのサービスには暗黙的にConflicts=ありますshutdown.target。したがってshutdown.target、発生すると、すべてのサービスが停止します。唯一の例外は追加する場合ですDefaultDependencies=no

あなたが要求した他の答えのコメントからreboot.target

   reboot.target
       A special target unit for shutting down and rebooting the system.

詳細を見ると、次の要因が原因であることがreboot.targetわかります。shutdown.targetsystemd1-reboot.service

$ busctl introspect \
    org.freedesktop.systemd1 \
    /org/freedesktop/systemd1/unit/reboot_2etarget
...
.Names    "reboot.target" "runlevel6.target" "ctrl-alt-del.target"
.Requires "systemd-reboot.service"

$ busctl introspect \
    org.freedesktop.systemd1 \
    /org/freedesktop/systemd1/unit/systemd_2dreboot_2eservice
...
.DefaultDependencies false
.After               "final.target" "system.slice" "umount.target" "shutdown.target" "systemd-journald.socket"
.Requires            "final.target" "system.slice" "umount.target" "shutdown.target"
.SuccessAction       "reboot-force"

したがって、あなたの場合は、「再起動時にサービスを停止するのを防ぐ」セクションDefaultDependencies=noに追加できますが、[Unit]シャットダウン中にサービスを停止することはできません。再起動すると終了も含まれます。


私の最善のアイデア(可能であるかどうかわからない)は、サービスを作成することです。

[Unit]
DefaultDependencies=no
Before=reboot.target shutdown.target

[Service]
ExecStart=touch %t/rebooting

[Install]
WantedBy=reboot.target

再起動が始まると、RAMにファイルが作成されます。

次に、サービスを次に変更します。

ExecStop=/bin/bash -c "test -e %t/rebooting || /usr/local/sbin/ur_shutdown"

これにより、ExecStopファイルの存在中にコマンドが実行されなくなります。これはコマンドの実行をブロックしませんが、ペリフェラルがExecStartすでに実行されている場合はすぐに返されると予想されます。

を実行したくない場合は、ExecStart不揮発性の場所にファイルを生成してみてください。

ExecStart=touch %S/rebooting

次に、スクリプトが存在しない場合にのみスクリプトを実行し、存在する場合はクリーンアップします。

ExecStart=/bin/bash -c "if [ -e %S/rebooting ]; then rm %S/rebooting; else /usr/local/sbin/ur_start; fi"
ExecStop=/bin/bash -c "test -e %S/rebooting || /usr/local/sbin/ur_shutdown"

おすすめ記事