Is there a way to check how many times linux service restarted in specific time range? Currently using
systemctl status nameOfService | grep -Po ".*; \K(.*)(?= ago)"
to extract uptime, but it's not enough for diagnostics.
If you have logs from that period, you can indeed extract the "Stopped"/"Started" messages from the logs. On nowadays system with systemd
, many systems use journalctl
, but many systems use something else - you'll have to inspect your specific system on how to query the logs.
Your script could look something along:
journalctl --since=<date> --until=<another data> UNIT=<service name> SYSLOG_IDENTIFIER=systemd |
awk '
/Started/{ started = 1 }
/Stopped/{ if (started) { started = 0; restarted++; } }
END {
print "It was restarted " restarted "times."
}
'
The regex here are just an example. Better use journalctl -o json
and parse the output with jq
.