注:私はサービスを作成する方法とこの特定の問題を回避する方法を説明する記事をMediumに書きました。systemdを使用したLinuxサービスの作成。
元の質問:
私はワーカースクリプトを常に実行するためにsystemdを使用しています。
[Unit]
Description=My worker
After=mysqld.service
[Service]
Type=simple
Restart=always
ExecStart=/path/to/script
[Install]
WantedBy=multi-user.target
数分後にスクリプトが正常に終了したら再起動しても問題はありませんが、起動時に繰り返し実行に失敗した場合は起動systemd
試行を放棄することを発見しました。
Jun 14 11:10:31 localhost systemd[1]: test.service: Main process exited, code=exited, status=1/FAILURE
Jun 14 11:10:31 localhost systemd[1]: test.service: Unit entered failed state.
Jun 14 11:10:31 localhost systemd[1]: test.service: Failed with result 'exit-code'.
Jun 14 11:10:31 localhost systemd[1]: test.service: Service hold-off time over, scheduling restart.
Jun 14 11:10:31 localhost systemd[1]: test.service: Start request repeated too quickly.
Jun 14 11:10:31 localhost systemd[1]: Failed to start My worker.
Jun 14 11:10:31 localhost systemd[1]: test.service: Unit entered failed state.
Jun 14 11:10:31 localhost systemd[1]: test.service: Failed with result 'start-limit'.
同様に、ワーカースクリプトが次の終了状態で複数回失敗した255
場合systemd
:
Jun 14 11:25:51 localhost systemd[1]: test.service: Failed with result 'exit-code'.
Jun 14 11:25:51 localhost systemd[1]: test.service: Service hold-off time over, scheduling restart.
Jun 14 11:25:51 localhost systemd[1]: test.service: Start request repeated too quickly.
Jun 14 11:25:51 localhost systemd[1]: Failed to start My worker.
Jun 14 11:25:51 localhost systemd[1]: test.service: Unit entered failed state.
Jun 14 11:25:51 localhost systemd[1]: test.service: Failed with result 'start-limit'.
systemd
強制的にできる方法はありませんかいつも再試行するには数秒かかりますか?
ベストアンサー1
Rahulの答えをもう少し拡張したいです。
systemdは何度も(StartLimitBurst
)再起動を試み、試行回数に達すると試行を停止しますStartLimitIntervalSec
。どちらのオプションもこのセクションに属します[unit]
。
実行間のデフォルト遅延は100ミリ秒(RestartSec
)これは速度制限に非常に迅速に到達する可能性があります。
systemd は、デバイスに対して自動的に再起動を試みません。再起動戦略の定義:
設定されて起動制限に達したデバイスは
Restart=
再起動を試みませんが、後で手動で再起動でき、この時点から再起動ロジックが再びアクティブになります。
遅延時間が長くなると、その時間内にエラーカウンタに到達できなくなり、Rahulの回答が役に立ちましたStartLimitIntervalSec
。正解は、とRestartSec
をすべてStartLimitBurst
合理的な値に設定することです。