Is there a command to check if the machine has been rebooted?
I have tried last reboot | head -3
and who -r
, but I noticed that both give the same result even when the machine is shut down.
Is there a command to check if the machine has been rebooted?
I have tried last reboot | head -3
and who -r
, but I noticed that both give the same result even when the machine is shut down.
If you want the time that the machine has been running since the last full shutdown or reboot, you can use uptime
. For example, on my laptop:
$ uptime
16:25:57 up 5 days, 1:47, 2 users, load average: 1.20, 1.04, 1.26
Note that this does not count the time the laptop was suspended. I usually suspend my laptop every night, but as you can see, it is still reporting more than 5 days of uptime, despite having a good night's sleep every night.
Use uptime
with the options -s
and -p
System up since: uptime -s
Systme up, running time: uptime -p
Check with last
and -x
-x
Display the system shutdown entries and run level changes.
last -x shutdown
last -x reboot
last -x reboot -3
Check with who
System up since: who -b
Executing this command will display a list of boots along with their boot IDs and timestamps. The most recent boot will be listed at the top. By comparing the boot IDs, you can easily determine if a reboot has occurred.
For systems that use the systemd init system, you can use the following command:
journalctl --list-boots
Alternatively, you can use the systemctl command in conjunction with the show option to retrieve the timestamp of the last boot. Specifically, you can use the following command:
systemctl show --property=ActiveEnterTimestamp systemd-udevd.service
This command fetches the timestamp of the systemd-udevd.service, which is started early in the boot process.
Both of these approaches provide a more precise and elegant solution compared to the previous methods, as they directly access system logs and systemd data. Choose the method that best fits your requirements and enjoy a more streamlined approach to checking if the machine has been rebooted.