Is there a way to tell how many times my computer has rebooted in a 24 hour period?
Solution 1
System reboots are usually logged in the binary file /var/log/wtmp
. The easiest way to read that file is using the last
command which prints lines like this for every reboot:
reboot system boot 3.10-3-amd64 Fri May 2 19:30 - 21:02 (5+01:32)
so if you issue last reboot
, you'll get a list of all reboots logged to your wtmp
file.
Solution 2
You can invoke directly
last reboot
The pseudo user reboot logs in each time the system is rebooted. Thus last reboot will show a log of all reboots since the log file was created.
it gives in addition the time from which starts the logfile (/var/log/wtmp
).
If you are interested in the past too, there are usually stored one or more files with past logs (/var/log/wtmp.1
)...
Here below a little example script to count the number of reboots and crashes in one day.
You can run it with /bin/bash Myscript.sh
to have the results for today
or /bin/bash Myscript.sh "Tue May 20"
to have results for the 20 of May
#!/bin/bash
Today=`date`
StrDay=${1:-${Today:0:10}}
N_Crash=`last -F |grep crash | grep "$StrDay" | sort -k 7 -u | wc -l`
N_Reboot=`last -F | grep reboot | grep "$StrDay" | wc -l `
echo "# Today $Today Report for crash and reboot on $StrDay "
echo "# Crashes $N_Crash"
echo "# Reboot $N_Reboot"
Notes:
with reboot you have few problem because there is one line each time you reboot.
This crash count is only indicative: you can have many lines (e.g. one for each shell) in case of crash. Even more, if the crash is not abrupt you can have different times for the same crash (maybe a second or a minute if it is in boundary). Moreover, even if this is more rare, you can make crash a shell without a crash of the system. The sort option try to kill duplicate lines for the same time. To have a correct result you should count as reboot only the lines of reboot without crashes between and as crashes the reboot with a crash before.
Alex
Updated on September 18, 2022Comments
-
Alex almost 2 years
I was doing a 24 hour code-a-thon and had to reboot numerous times and was wondering if there was a way to tell exactly how many the computer was rebooted or crashed.
-
slm about 10 yearsSomewhat related: Is there a tool for tracking uptimes across reboots?
-
-
user80551 about 10 yearsNo need to grep, just
last reboot
displays the entries by the "user"reboot
. -
mreithub about 10 yearsThat's true, I looked over that little detail when reading
last
's manpage today (I was expecting it to be a parameter value, not a simple argument...)