1

Currently, I have my server output my uptime to a HTML page using:

TIME=$(uptime -p) echo ""${TIME}"!" >> /var/www/html/index.new

Which generates an output of:

up 1 day, 1 hour, 2 minutes!

I would like to also (for the sake of curiosity) to be able to display my system's record uptime, though am uncertain as to the best way to log this and display it back in the (uptime -p) [day, hr, min] format.

Is there a pre-existing tool which can do this? Or would I need to log uptime to a file and pull out the highest value with grep or something similar?

0

After installing 'uptimed', I have added the following few lines to my script:

uprecords | head -n 4 | tail -n 1 | awk '{print "Current uptime record is " $3,$4$5,$6" (hr:mis:sec)"}'

Current uptime record is 1 day, 02:05:34 (hr:min:sec)

This should be more than suitable for my needs.

0

I would suggest using the -s flag to get a parsable time of when the system started. And then use date and subtraction.

Keep doing this in a loop and compare it against the record. The record time needs to be saved to file of course.

(The record file needs to be initialized. echo 0 > record)

As long as this script keeps running, anything can simply read the record file to find out what the current record is.

#!/bin/sh

format_time ()
{
    t=$1

    days=$(($t / 86400))
    t=$(($t - 86400*$days))

    hours=$(($t / 3600))
    t=$(($t - 3600*$hours))

    minutes=$(($t / 60))
    t=$(($t - 60*$minutes))

    echo "$days days $hours hours $minutes minutes $t seconds"
}

main ()
{
    # Old record
    record="$(cat record)"

    # When the system started.
    boot="$(date -d "$(uptime -s)" '+%s')"
    while true; do
        # You could also calculate the uptime before the loop and just
        # increment it, but that will rely on the script running 24/7.
        now="$(date '+%s')"
        current_uptime="$(($now - $boot))"
        # Set and save the record when it gets beaten
        if [ "$current_uptime" -gt "$record" ]; then
            record="$current_uptime"
            echo "$record" > record
        fi
        # Status should be printed _after_ potentially having set
        # a new record:
        clear
        echo "Current uptime: $(format_time "$current_uptime")"
        echo "Record uptime: $(format_time "$record")"
        # Don't waste system resources for silly things.
        sleep 1
    done
}

main
0

Since you're already using uptimed, you could also install uprecords-cgi which presents the information stored by uptimed in a web page; by default it will be enabled at http://localhost/cgi-bin/uprecords.cgi.

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.