diff --git a/doc/ChangeLog.md b/doc/ChangeLog.md index 3dba85e1..1f191dbb 100644 --- a/doc/ChangeLog.md +++ b/doc/ChangeLog.md @@ -14,6 +14,11 @@ All relevant changes are documented in this file. - The `restart_sec:` option of the line-based format is now spelled `restart-sec:`, kebab-case like every other setting. The old spelling is still accepted +- `initctl -j status` now has all `initctl` gets from Finit about a + service. New is a `restart` object with the restart counter, + `restart-max`, the current restart delay and its limit, and `manual` + and `started`. `starts` is now there for all services, not only those + with `manual-start` - 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 diff --git a/doc/initctl.md b/doc/initctl.md index 20380eb9..19063220 100644 --- a/doc/initctl.md +++ b/doc/initctl.md @@ -146,6 +146,53 @@ Apr 8 15:02:11 alpine authpriv.info dropbear[2634]: Child connection from 192.1 Apr 8 15:02:12 alpine authpriv.notice dropbear[2634]: Password auth succeeded for 'root' from 192.168.121.1:48576 ``` +With `-j` the same command gives JSON, for scripts and other programs. +The view above is kept short for reading, the JSON has everything +`initctl` gets from Finit about the service: + +``` +alpine:~# initctl -j status dialer +{ + "identity": "dialer", + "description": "Modem dialer", + "type": "service", + "forking": false, + "status": "restart", + "exit": { "code": 1 }, + "origin": "/etc/finit.d/enabled/dialer.conf", + "command": "dialer", + "manual": false, + "started": false, + "starts": 0, + "restarts": 7, + "restart": { + "count": 4, + "max": -1, + "delay": 160, + "limit": 300 + }, + "pidfile": "none", + "pid": 0, + "user": "root", + "group": "root", + "uptime": 0, + "runlevels": [ 2, 3, 4, 5 ] +} +``` + +In `restart`, `count` is the restart counter that `restart-max`, here +`max`, is compared to, -1 for `restart = "always"`. The total since +boot is in `restarts`. `delay` is the number of seconds before the +next restart, should the service crash, and `limit` is its +`restart-max-sec`, 0 when not set. `environment`, `condition`, `exit`, +and the cgroup members `cgroup`, `memory`, `cpu`, and `limits` are left +out when they do not apply. + +``` +alpine:~# initctl -j status dialer | jq .restart.delay +160 +``` + Switch Root ----------- diff --git a/src/initctl.c b/src/initctl.c index b5749c60..0f15a4c3 100644 --- a/src/initctl.c +++ b/src/initctl.c @@ -1755,12 +1755,27 @@ static int json_status_one(FILE *fp, struct svc_row *r, char *indent, int prev) fprintf(fp, "%s \"condition\": %s,\n", indent, buf); - if (r->manual) - fprintf(fp, - "%s \"starts\": %u,\n", indent, r->starts); - fprintf(fp, - "%s \"restarts\": %d,\n", indent, r->restart_tot); /* XXX: add restart_cnt and restart_max */ + "%s \"manual\": %s,\n" + "%s \"started\": %s,\n" + "%s \"starts\": %u,\n" + "%s \"restarts\": %d,\n" + "%s \"restart\": {\n" + "%s \"count\": %d,\n" + "%s \"max\": %d,\n" + "%s \"delay\": %u,\n" + "%s \"limit\": %u\n" + "%s },\n", + indent, r->manual ? "true" : "false", + indent, r->started ? "true" : "false", + indent, r->starts, + indent, r->restart_tot, + indent, + indent, r->restart_cnt, + indent, r->restart_max, + indent, r->restart_delay, + indent, r->restart_delay_max, + indent); /* Add memory and CPU information if cgroup support is available */ if (cgrp && r->pid > 1) { diff --git a/test/restart-backoff.sh b/test/restart-backoff.sh index de5cad7f..37040b58 100755 --- a/test/restart-backoff.sh +++ b/test/restart-backoff.sh @@ -77,6 +77,8 @@ 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)" +run "initctl -j status crash" +assert "Same in JSON" "$(texec initctl -j status crash | jq -c .restart)" = '{"count":2,"max":-1,"delay":4,"limit":4}' assert "Restart counter is 2" "$(texec initctl status crash | awk '/Restarts/{ print $4 }')" = "(2/-1)" sep