Find the last boot and shutdown on Linux
Using last, who, ausearch and the systemd journal to date the last boot and tell a graceful stop from an ungraceful one.
Several commands show when a Linux host last booted and how it went down. Graceful and crash reboots leave different traces, so the log records help tell them apart.
Read the shutdown and boot records with last
last -x | grep shutdown | head -1
last -n 2 -x shutdown reboot
last -x | head | tac
last reboot
-x adds the system shutdown and runlevel-change entries to the output.
- After a graceful reboot, a
shutdownrecord appears before the newbootrecord. - After a crash or a forced power-off, a
bootrecord follows the previousbootrecord with noshutdownin between.
Boot time from uptime
date -d "$(</proc/uptime awk '{print $1}') seconds ago"
The first field of /proc/uptime is the number of seconds since boot.
Boot time with who
who -b
Boot and shutdown audit records
sudo ausearch -i -m system_boot,system_shutdown | tail -4
This needs the audit subsystem (auditd) to be running and to have logged these events.
Keep the systemd journal across reboots
Without a persistent journal, the logs of the previous boot are lost at reboot. Either set Storage=persistent in /etc/systemd/journald.conf, or create the directory and restart the journal:
sudo mkdir /var/log/journal
sudo systemd-tmpfiles --create --prefix /var/log/journal 2>/dev/null
sudo systemctl -s SIGUSR1 kill systemd-journald
Force a reboot with the SysRq key
Use this only on a test machine or as a last resort. These commands act immediately and do not sync or unmount file systems.
echo 1 > /proc/sys/kernel/sysrq # enable all SysRq functions
echo b > /proc/sysrq-trigger # reboot immediately
echo o > /proc/sysrq-trigger # power off immediately
The list of SysRq commands is described on the Magic SysRq key page.
Notes
- Run the
echocommands as root on the target host, for example throughssh root@<host>followed by the commands.