total uptime between shutdowns

total uptime between shutdowns

Tom Rosenfeld trosenfeld at gmail.com
Tue Feb 1 17:46:07 IST 2011


Thanks for all your tips!

I found that the information is basically available in 'last -x'.

Below is  a rough script I put together to estimate total times rounded to
hours:

Thanks,
-tom

--------------------------------

#!/bin/bash


#
# Compute total hours machine has been running, minus time it was turned
off.
# Based on output from 'last -x'
#


STOP_F=/tmp/stop
START_F=/tmp/start

last -x |awk '/shutdown/ {print $5, $6, $7, $8}' |tac  > $STOP_F
last -x |awk '/reboot/ {print $5, $6, $7, $8}'  |tac > $START_F
# Start times (should be 1 more than shutdown times):

# get rid of 1st start line since machine is still up
sed -i '1d' $START_F

#
# Read 1 line from each file and compare
# (see redirection at end of while loop
#
while read STOP <&8
do
        read START <&7

        # compute time machine was stopped
        start_s=$(date -d "$START" +%s)
        stop_s=$(date -d "$STOP" +%s)

        ((duration = start_s - stop_s))
        ((tot_hours = duration / (60*60) ))

        echo total hours stopped = $tot_hours
        ((saved_hours+=tot_hours))

done 7<$START_F 8<$STOP_F

#
# Compute total uptime since first boot
#

# seconds at 1st boot
first_s=$( date -d "$(last |tail -1 | awk '{$1=""; $2="" ;print }')" +%s)
now_s=$(date  +%s)

((elapsed_hours= (now_s - first_s) /(60*60) ))
echo Total elapsed hours = $elapsed_hours
echo Saved hours = $saved_hours
echo Total charged hours = $((elapsed_hours - saved_hours))
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.cs.huji.ac.il/pipermail/linux-il/attachments/20110201/98fb141e/attachment.html>


More information about the Linux-il mailing list