ExecStartPreの実行結果に基づいてsystemdサービスを開始する方法

ExecStartPreの実行結果に基づいてsystemdサービスを開始する方法

起動プロセス中にsystemdサービスファイルを使用して起動されるデーモンプロセスがあります。スクリプトの実行結果に応じてデーモンプロセスを開始したいと思います。このスクリプトは、ExecStartPreオプションの下のサービスファイルに含まれています。

に基づいてスクリプト実行結果、次のようにサービスを処理する必要があります。

  1. スクリプトが返される場合0. サービスを開始して起動を続行する
  2. もし1返され、サービスを停止して継続しないでください。
  3. もし2返され、サービスを開始しないでください。

私のシナリオがうまくいくかどうか知りたいです。それでは、これを達成する方法は何ですか?

よろしくお願いします。

ベストアンサー1

systemd.service(5)説明する:

  ExecStartPre=, ExecStartPost=
      Additional commands that are executed before or after the command in
      ExecStart=, respectively. Syntax is the same as for ExecStart=, except that
      multiple command lines are allowed and the commands are executed one after the
      other, serially.

      If any of those commands (not prefixed with "-") fail, the rest are not
      executed and the unit is considered failed.

      ExecStart= commands are only run after all ExecStartPre= commands that were not
      prefixed with a "-" exit successfully.

その結果、ゼロ以外の終了コードがサービスに失敗する可能性があります。これはリストの要件 1 と 3 を満たします。つまりExecStartPre=、0が返されるとサービスが開始され、すべてが引き続き開始されます。 0が返されない場合、ExecStartPre=サービスは失敗したとマークされ、他のすべては引き続き開始されます。に電話してsystemctl statusくださいState: degraded

条件2は少し混乱しています。 「起動を続行しない」とは、「終了」または「systemctl stop multi-user.target」を意味しますか?このサービスが開始されないと、システムをシャットダウンしてログインして問題を解決することはできません(何らかの種類の回復モードに入らない限り)。どのように停止したりmulti-user.target起動したりしても、shutdown.target終了コードに従って簡単に行うことはできませんExecStartPre=ExecStartPre=元の行を呼び出して終了コードを処理するシェルスクリプトにコードを置き換える必要があります。

私はあなたが似たようなものが欲しいと思いますRestart=on-failureRestartForceExitStatus=または、以下を使用して、基本プロセスの終了コードに従ってサービスを再起動するか、再起動を防ぐこともできます。RestartPreventExitStatus=

おすすめ記事