Is there a way to determine whether a RHEL7 server was rebooted via systemctl (or reboot / shutdown aliases), or whether the server crashed? Pre-systemd this was fairly easy to determine with last -x runlevel
, but with RHEL7 it's not so clear.
Is there a way to determine whether a RHEL7 server was rebooted via systemctl (or reboot / shutdown aliases), or whether the server crashed? Pre-systemd this was fairly easy to determine with last -x runlevel
, but with RHEL7 it's not so clear.
Funny, I just happened to reboot a CentOS 7 system last night, and so I have a nice log of just this to look at.
In the case of a crash, obviously nothing is logged between the time of the crash and the system restart.
In the case of a reboot, it is pretty obvious, as you get a log of (nearly) everything systemd is doing to shut down the system.
One such log entry you aren't likely to see in any circumstance other than shutting down or going to single-user mode is:
You can reboot your own system to see what actually gets logged.
There's more than one way to do this, but I'll cover the 4 best ones I can think of. (EDIT: I published a cleaned-up version of this as a public article on redhat.com. See: How to distinguish between a crash and a graceful reboot in RHEL 7.)
(1) auditd logs
auditd is amazing. You can see all the different events that it logs by checking
ausearch -m
. Apropos to the problem at hand, it logs system shutdown and system boot, so you can use the commandausearch -i -m system_boot,system_shutdown | tail -4
. If this reports a SYSTEM_SHUTDOWN followed by a SYSTEM_BOOT, all is well; however, if it reports 2 SYSTEM_BOOT lines in a row, then clearly the system did not shutdown gracefully, as in the following example:(2) last -x
Same as above, but with the simple
last -n2 -x shutdown reboot
command. Example where system crashed:Or where system had a graceful reboot:
(3) create your own service unit
This is IMHO the best approach because you can tailor it to whatever you want. There are a million ways to do this. Here's one I just made up. This next service only runs at shutdown.
Then when the system boots, this next service will only start if the file created by the above shutdown service exists.
So at any given time I can check if the previous boot was done after a graceful shutdown by doing
systemctl is-active check_graceful
, e.g.:Or here's after an ungraceful shutdown:
(4) journalctl
It is worth mentioning that if you configure
systemd-journald
to keep a persistent journal, you can then usejournalctl -b -1 -n
to look at the last few (10 by default) lines of the previous boot (-b -2
is the boot before that, etc). Example where the system rebooted gracefully:If you get good output like that, then clearly the system was shutdown gracefully. That said, it's not super-reliable in my experience when bad things happen (system crashes). Sometimes the indexing gets weird.
I don't particularly like the answer, but it's an answer we got from RH. I'm posting it here in case it helps someone else.
One possible way is to grep for
rsyslogd
in/var/log/messages
. A graceful shutdown would haveexiting on signal 15
. A crash would not.tac /var/log/messages | grep 'rsyslogd.*start\|rsyslogd.*exit'
Two consecutive
start
lines may indicate a crash. And astart
followed by anexit
may indicate a reboot.Unfortunately it could also give bad results if rsyslogd goes down or is restarted outside a reboot/crash.
This seems to work consistently for "graceful shutdowns" (
shutdown
,reboot
,systemctl
) as well as "crashes" (power off, reset,echo c > /proc/sysrq-trigger
):last -x | grep 'reboot\|shutdown'
A
reboot
line followed by ashutdown
line indicates a "graceful shutdown". Tworeboot
lines indicates a "crash".