From 308287923db510f410750ee3b91567e72db6395d Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Thu, 19 Nov 2026 19:29:43 +0100 Subject: [PATCH] initctl: show the restart delay of a service with restart-max-sec With restart-max-sec the delay before the next restart varies, and nothing showed what it was or how far it could grow. Show both in initctl status, for a service that has the setting: Restarts : 7 (4/-1) Restart in : 160 sec (max 300) Add the D-Bus properties RestartDelay and RestartDelayMax for it, so the status is the same when initctl goes over D-Bus. Signed-off-by: Joachim Wiberg --- doc/ChangeLog.md | 4 +++- doc/config/service-opts.md | 12 +++++++++++- doc/dbus.md | 2 ++ src/dbus.c | 4 ++++ src/initctl.c | 9 +++++++++ test/restart-backoff.sh | 1 + 6 files changed, 30 insertions(+), 2 deletions(-) diff --git a/doc/ChangeLog.md b/doc/ChangeLog.md index 656c31a9..3dba85e1 100644 --- a/doc/ChangeLog.md +++ b/doc/ChangeLog.md @@ -17,7 +17,9 @@ All relevant changes are documented in this file. - Add `restart-max-sec`, a limit for the delay between restarts of a crashing service. When set, the delay doubles for each restart, from `restart-sec` up to the limit, and comes down again as the service keeps - running. Issue #306 + running. `initctl status` shows the current delay and the limit, also + available as the D-Bus properties `RestartDelay` and `RestartDelayMax`. + Issue #306 - Add `stop-scope`, which processes a stop applies to: `pgroup`, the process group of the main process (default), `process`, only the main process, `cgroup`, the cgroup of the service, or `mixed` like systemd. Issue #413 diff --git a/doc/config/service-opts.md b/doc/config/service-opts.md index ef4a16e7..345567f4 100644 --- a/doc/config/service-opts.md +++ b/doc/config/service-opts.md @@ -186,7 +186,8 @@ following settings are available: restart is at once, then after 2, 4, 8 seconds, and so on. A service that stays up longer than the current delay is restarted after `restart-sec` the next time it crashes, and the delay comes down - again the longer the service keeps running. Useful with + again the longer the service keeps running. `initctl status NAME` + shows the current delay and the limit, see below. Useful with `restart = "always"`, for a service that should be retried forever, only less and less often * `restart = "always"` -- no upper limit on the number of times Finit @@ -208,6 +209,15 @@ following settings are available: * `oncrash = "script"` -- similarly, but instead of rebooting, call the `exec-stop-post` script with exit code `crashed`, see below +With `restart-max-sec` set, `initctl status` shows the delay before the +next restart and its limit, here for a service that has crashed four +times in a row: + + $ initctl status dialer + ... + Restarts : 7 (4/-1) + Restart in : 160 sec (max 300) + Service directories ------------------- diff --git a/doc/dbus.md b/doc/dbus.md index 36422274..1e9ebdb3 100644 --- a/doc/dbus.md +++ b/doc/dbus.md @@ -187,6 +187,8 @@ All read-only; observable via `Properties.Get` and `Properties.GetAll`. | `ExitStatus` | `u` | Raw `waitpid(2)` status from the last exit | | `RestartsTotal` | `u` | Restarts over the service's lifetime | | `RestartMax` | `u` | Restart limit before the service is blocked | +| `RestartDelay` | `u` | Seconds before the next restart, should the service crash | +| `RestartDelayMax` | `u` | The `restart-max-sec` setting, 0 when not set | | `Starts` | `u` | Times started, for `manual-start` units | | `ManualStart` | `b` | `manual-start` set in the `.conf` | | `Forking` | `b` | Daemon forks to background | diff --git a/src/dbus.c b/src/dbus.c index ae8c3566..efc10beb 100644 --- a/src/dbus.c +++ b/src/dbus.c @@ -797,6 +797,8 @@ SVC_PROP_U32 (svc_prop_runlevels, runlevels) SVC_PROP_U32 (svc_prop_exitstatus, status) SVC_PROP_U32 (svc_prop_restarts_tot, restart_tot) SVC_PROP_U32 (svc_prop_restart_max, restart_max) +SVC_PROP_U32 (svc_prop_restart_delay, restart_tmo / 1000) /* msec */ +SVC_PROP_U32 (svc_prop_restart_delay_max, restart_tmo_max / 1000) SVC_PROP_U32 (svc_prop_starts, once) SVC_PROP_BOOL(svc_prop_manual, manual) SVC_PROP_BOOL(svc_prop_forking, forking) @@ -822,6 +824,8 @@ static const link_property_t service_properties[] = { { .name = "ExitStatus", .sig = "u", .getter = svc_prop_exitstatus }, { .name = "RestartsTotal",.sig = "u", .getter = svc_prop_restarts_tot }, { .name = "RestartMax", .sig = "u", .getter = svc_prop_restart_max }, + { .name = "RestartDelay", .sig = "u", .getter = svc_prop_restart_delay }, + { .name = "RestartDelayMax", .sig = "u", .getter = svc_prop_restart_delay_max }, { .name = "Starts", .sig = "u", .getter = svc_prop_starts }, { .name = "ManualStart", .sig = "b", .getter = svc_prop_manual }, { .name = "Forking", .sig = "b", .getter = svc_prop_forking }, diff --git a/src/initctl.c b/src/initctl.c index 7b746666..b5749c60 100644 --- a/src/initctl.c +++ b/src/initctl.c @@ -1495,6 +1495,8 @@ struct svc_row { int32_t restart_tot; int32_t restart_cnt; int32_t restart_max; /* -1 = restart:always */ + uint32_t restart_delay; /* seconds, before the next restart */ + uint32_t restart_delay_max; /* restart-max-sec, 0 = not set */ int manual; int forking; int started; @@ -1525,6 +1527,8 @@ static void fill_row_from_svc(struct svc_row *r, svc_t *svc) r->restart_tot = (int32_t)svc->restart_tot; r->restart_cnt = (int32_t)svc->restart_cnt; r->restart_max = (int32_t)svc->restart_max; + r->restart_delay = (uint32_t)svc->restart_tmo / 1000; + r->restart_delay_max = (uint32_t)svc->restart_tmo_max / 1000; r->manual = svc->manual; r->forking = svc->forking; r->started = svc->started; @@ -1940,6 +1944,9 @@ static int show_one_row(struct svc_row *r) printf(" Starts : %u\n", r->starts); printf(" Restarts : %d (%d/%d)\n", r->restart_tot, r->restart_cnt, r->restart_max); + if (r->restart_delay_max) + printf(" Restart in : %u sec (max %u)\n", + r->restart_delay, r->restart_delay_max); printf(" Runlevels : %s\n", runlevel_string(runlevel, (int)r->runlevels)); @@ -2055,6 +2062,8 @@ static const struct row_field { ROW_U32 ("RestartsTotal", restart_tot), ROW_U32 ("RestartCount", restart_cnt), ROW_U32 ("RestartMax", restart_max), + ROW_U32 ("RestartDelay", restart_delay), + ROW_U32 ("RestartDelayMax", restart_delay_max), ROW_BOOL("ManualStart", manual), ROW_BOOL("Forking", forking), ROW_BOOL("Started", started), diff --git a/test/restart-backoff.sh b/test/restart-backoff.sh index a6cb1b2c..de5cad7f 100755 --- a/test/restart-backoff.sh +++ b/test/restart-backoff.sh @@ -76,6 +76,7 @@ assert_delay 3 4 4 5 assert_delay 4 5 4 5 say 'The restart counter stops where the delay peaks' +assert "Delay and limit in status" "$(texec initctl status crash | sed -n 's/.*Restart in : //p')" = "4 sec (max 4)" assert "Restart counter is 2" "$(texec initctl status crash | awk '/Restarts/{ print $4 }')" = "(2/-1)" sep