systemdは、他のサービスがエラーなしで実行されている場合にのみサービスを開始します。

systemdは、他のサービスがエラーなしで実行されている場合にのみサービスを開始します。

エラーなしで他のサービスユニットが実行されている間にのみサービスユニットを起動するには?

2つのサービス単位があります。

#echo-date-0.service
[Unit]
Description=

[Service]
ExecStart=/home/user/bash/echo-date-0.sh

[Install]
WantedBy=multi-user.target



#echo-date-1.service
[Unit]
Description=

[Service]
ExecStart=/home/user/bash/echo-date-1.sh
Requires=echo-date-0.service
After=echo-date-0.service

[Install]
WantedBy=multi-user.target

私のスクリプトecho-date-0.shは終了コード1(exit 1)を返し、echo-date-0.serviceのステータスを確認すると次のようになります。

Active: failed (Result:exit-code) 
Process: (code=exited, status=1/FAILURE)

ただし、echo-date-0.serviceが必要ですが、echo-date-1.serviceも実行されます。 echo-date-0.serviceが失敗した場合にecho-date-1.serviceの実行を停止する方法は?

ベストアンサー1

#echo-date-0.service
[Unit]
Description=

[Service]
Type=oneshot
## Stay alive for other services to acknowledge 
RemainAfterExit=yes
ExecStart=/home/user/bash/echo-date-0.sh

[Install]
WantedBy=multi-user.target



#echo-date-1.service
[Unit]
Description=
Requires=echo-date-0.service
After=echo-date-0.service
## A unit that must be in an active non-erroring state
## and combos great with After=
BindsTo=echo-date-0.service

[Service]
ExecStart=/home/user/bash/echo-date-1.sh

[Install]
WantedBy=multi-user.target

Jesse_b少なくとも私にとってはBindsTo=Unixシステム役に立ちます。

そしてビゴンセクションに&[Unit]が含まれていることが正しく指定されています。Requires=After=

実際、echo-date-0.service上記の(動作することを望む)例では、生きるサービスに(したがってsystemctl stop echo-date-0.service状態をリセットするには実行する必要があります)、このように設定してAfter=&を組み合わせて間違えないサービスを指すようにしますBindsTo=echo-date-1.serviceしなければならないOPが要求したものをここで実装してください。

この場合、どのような形で接続されたサービスを表示するには、引き続きsystemctl list-dependencies --before <service/trigger>実行できる必要があります。echo-date-0.service

おすすめ記事