Speed8ump
Speed8ump

Reputation: 1317

How to provide a restart count to systemd service

I have an embedded device which manages its various services using systemd. Our status reporting application is one of these services. It is always on and it automatically restarts on failure (crashes, exceptions, OOM conditions, whatever).

We report an event to our cloud services on device restart (technically application restart) but I'd like to distinguish first start (after reboot) from restart. Is there a mechanism built into systemd which can provide the service restart count, or do I need to roll my own method?

Upvotes: 4

Views: 3828

Answers (2)

Mohsen Zahraee
Mohsen Zahraee

Reputation: 3483

you can use 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.

Upvotes: 3

Aissen
Aissen

Reputation: 1536

Do you have the journal ? If you do, then you can get the count like this:

journalctl -b -u myservicename.service |grep -c Started

The -b option limits logs to the current boot; -u limits to the service in argument.

Then you grep for the "Started" line, and tell grep to only give you the number of matches.

Upvotes: 2

Related Questions