CLI AI

Restart a service and actually verify it came back

2026-07-18

sudo systemctl restart nginx && systemctl is-active nginx restarts the service and prints one word telling you whether it is really running. The restart itself is silent on success and equally silent when the process dies a second later, so the check has to be its own command — and is-active answers inactive for a misspelled unit name too, which is where most people fool themselves.

1. Restart, then check in the same line

clai
$ clai restart docker and tell me if it came back→ sudo systemctl restart docker && systemctl is-active dockeractive

restart prints nothing when it succeeds, and silence is not proof of anything. is-active returns a single word with exit code 0, which is exactly what you want after &&.

2. The trap: a typo in the unit name

clai
$ clai check whether the nosuch service is running→ systemctl is-active nosuch.serviceinactive

A unit that does not exist answers inactive with exit code 4 — not "unit not found". In a script that reads as "the service is stopped" when in fact you mistyped the name. status is honest about it: Unit nosuch.service could not be found.

3. What status is actually telling you

clai
$ clai show the state of the docker service→ systemctl status docker● docker.service - Docker Application Container Engine     Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; preset: enabled)     Active: active (running) since Mon 2026-08-24 14:15:54 +07; 3 days ago Invocation: ca41df104d0f46f5a69c964621baf011TriggeredBy: ● docker.socket       Docs: https://docs.docker.com

Two lines carry the meaning. Loaded says the unit file was found, and enabled means it starts at boot. Active carries the uptime: if it says seconds when you restarted a minute ago, the service crashed and came back on its own.

4. A check for scripts, not for eyes

clai
$ clai show docker state in machine readable form→ systemctl show -p ActiveState,SubState,MainPID,NRestarts dockerActiveState=activeSubState=runningMainPID=5425NRestarts=0

show doesn't format anything for humans, so it survives systemd upgrades that reshuffle the pretty output. NRestarts climbing is the fastest signal that a Restart= policy is papering over a crash loop.

5. Find everything that failed

clai
$ clai show failed services→ systemctl --failed  UNIT              LOAD   ACTIVE SUB    DESCRIPTION● logrotate.service loaded failed failed Rotate log files

Short and to the point. For a single unit, systemctl is-failed <name> prints failed and exits 0 when the unit really is in that state.

6. Clear the failed state

clai
$ clai clear the failed state of the logrotate service→ sudo systemctl reset-failed logrotate.service

Until the failed state is cleared, the unit stays in the --failed list and the StartLimitBurst counter can refuse new attempts with "start request repeated too quickly". reset-failed zeroes the counter and removes the unit from the list.

Gotchas

  • Silence after restart proves nothing. systemctl restart prints an empty line both when the service is healthy and when it died one second in. The verification is always a separate command.
  • is-active can't tell a typo from a stopped service. Both answer inactive. The exit codes differ — 4 for a unit that doesn't exist — but you won't see that by eye, so reach for status whenever the answer surprises you.
  • restart does not re-read a changed unit file. After editing a .service file you need sudo systemctl daemon-reload, otherwise systemd starts the old version and you go hunting for a bug in a config you already fixed.

Related questions

What's the difference between restart and reload? reload asks the service itself to re-read its config without dropping connections; restart kills the process and starts it again. When a unit has no reload support, reload-or-restart picks whatever exists.

How do I see why a service didn't come up? journalctl -u <name> -n 50 -o cat shows the last lines of its log without the timestamp prefixes — the reason is usually right there.

How do I know it will survive a reboot? systemctl is-enabled <name> answers enabled or disabled. A service running right now can still be disabled and never come back after a restart of the machine.

See also

CliAI turns "restart it and tell me if it came back" into both commands, and shows them before either one runs. Install it in one line.