systemdサービスを開始し、再起動時に停止します。

systemdサービスを開始し、再起動時に停止します。

デフォルトでは、systemdがサービスを再起動するように要求された場合は、最初のプロセスを停止してから2番目のプロセスを開始するため、タイムラインは次のようになります。

PROCESS A
--------[stopped]
                    [started]--------
                    PROCESS B

サービスのダウンタイムに時間を費やすことはできませんが、両方のサービスを一緒に開始するのは大丈夫ですので、タイムラインを次のように表示したいと思います。

PROCESS A
------------------[stopped]
        [started]------------------
        PROCESS B

どうすればいいですか?

ベストアンサー1

解決策を提案します。サービスの複数のインスタンスを作成して最初のインスタンスを停止する前に、2番目のインスタンスを起動します。

たとえば、次の名前のユニットファイルを設定します(必須)。/etc/systemd/system/[email protected]@

[Install]
# for example
WantedBy=multi-user.target

[Unit]
# for example
Description=mymonitor %i

[Service]
# for example; the %i may not be useful in your case
ExecStart=/path/to/monitor %i

それを発見:

sudo systemctl daemon-reload

活性化:

sudo systemctl enable mymonitor@thisone

...そして起動時にインスタンス名 "thisone"でモニターを起動します。

サービスを再起動したい場合は、スクリプトを使用して簡単なテストを実行できます。

#!/bin/sh
if systemctl is-active mymonitor@thisone > /dev/null
then
  systemctl start mymonitor@thatone &&
  systemctl stop mymonitor@thisone
else if systemctl is-active mymonitor@thatone
  systemctl start mymonitor@thisone &&
  systemctl stop mymonitor@thatone
else
  echo Houston, we have a problem
  # or perhaps you want to start, with: systemctl start mymonitor@thisone
fi

開始と停止の間にさらに重複が必要な場合は、次のようにビルドしてください。

# ...
if systemctl start mymonitor@thisone
then
  sleep 7
  systemctl stop mymonitor@thatone
fi
# ...

おすすめ記事