Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 650
Speed8ump Avatar asked Nov 16 '25 20:11

Speed8ump


2 Answers

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.

like image 88
sayyed mohsen zahraee Avatar answered Nov 18 '25 18:11

sayyed mohsen zahraee


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.

like image 20
Aissen Avatar answered Nov 18 '25 18:11

Aissen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!