5

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.

11

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.

  • 1
    No need to grep, just last reboot displays the entries by the "user" reboot. – user80551 May 25 '14 at 17:39
  • That'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...) – mreithub May 25 '14 at 20:27
8

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.

0

Easy to know with tuptime, let me give you an example:

Tuptime track the shutdown / startup behaviour of the system, also, it allow filtering their report since a given timestamp.

Well, as the last 24 hours are 86400 seconds, the execution line with the seconds time in negative and the date command as reference will be:

$ date && tuptime  --tsince -86400
dom ene  7 10:13:06 CET 2018
System startups:    3   since   10:13:06 06/01/18
System shutdowns:   2 ok   -   0 bad
System uptime:      3.42 %   -   49 minutes and 16 seconds
System downtime:    96.58 %   -   23 hours, 10 minutes and 44 seconds
System life:        1 day, 0 hours, 0 minutes and 0 seconds
...

An other interesting output is passing the table option:

$ tuptime  --tsince -86400 -t
No.         Startup Date                      Uptime       Shutdown Date   End                              Downtime

78                                                                        OK    3 hours, 23 minutes and 11 seconds
79   13:51:57 06/01/18   10 minutes and 54 seconds   14:02:51 06/01/18    OK   19 hours, 31 minutes and 53 seconds
80   09:34:44 07/01/18    54 minutes and 2 seconds
0

I made a simpe shell script which will show you number of times our system rebooted a day.

#!/bin/bash
day=$(date | awk {'print $1 " " $3 " " $2'})
echo $day
reboot_times=$(last reboot | grep "$day" | wc -l)
echo $reboot_times times rebooted

output

Wed

2 times rebooted

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.