Can systemd show how many times a service restarted?
The counter has since been added and can be accessed with the following command:
systemctl show foo.service -p NRestarts
It will return a value if the service is in a restart loop, otherwise, will return nothing.
https://github.com/systemd/systemd/pull/6495
systemctl shows the last start time (or uptime) of the service. But that's not enough for telling how many times the service was restarted.
The following command can probably show you the number of starting (or restart) occurrences:
journalctl -u <service_name>
.service | grep Starting
For example:
$ journalctl -u foo.service | grep Starting
Jul 24 16:09:56 10.16.26.1 systemd[1]: Starting foo...
Jul 25 18:12:14 10.16.26.1 systemd[1]: Starting foo...
And this command just gives you a count of the number of restarts:
journalctl -u <service_name>
.service | grep Starting | wc -l
For example:
$ journalctl -u foo.service | grep Starting | wc -l
2
Note: you can vary the string you use here. You could use this 'Started' to tell if the service successfully started. Or you could use '<service_name>
.service failed' to tell the number of occurrences that the service failed.
Also, its interesting to note that they've been discussing the idea of adding a counter to systemd on GitHub:
https://github.com/systemd/systemd/issues/4126