LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Shell script to store high water mark for uptime. (https://www.linuxquestions.org/questions/programming-9/shell-script-to-store-high-water-mark-for-uptime-676544/)

McSlack 10-15-2008 11:27 AM

Shell script to store high water mark for uptime.
 
I wrote a small shell script that reads /proc/uptime and prints it in human viewable time. I'd like to somehow store the longest uptime. Here's what I've got so far.

Code:

#!/bin/csh
#
#
# Author D. McIntyre  07/31/08  mac@yestermac.com
#
# Modified 10/15/08 to work with Slackware linux. Original worked with netBSD.

# To avoid a "Badly Formed Number" error the following line converts uptime from float to integer

set systime = `cat /proc/uptime | awk 'BEGIN { FS = "." } ; { print $1 } '  `

# divide systime by 60, the remainder is minutes; divide systime by 3600 the remainder is hours
# divide systime by 86400 equals days.

@ minutes = ($systime / 60)  % 60
@ hours = ($systime / 3600) % 24 
@ days =  $systime / 86400

#  If day, hour or minute equals 1 then print singular version. 

if ($days == 1  ) then
  set day="day, "
    else
    set day="days, "
endif

if ($hours == 1 ) then
  set hour="hour and "
    else
    set hour="hours and "
endif

if ($minutes == 1 ) then
  set minute=" minute. "
    else
    set minute=" minutes. "
endif

echo  "This system has been up" $days $day $hours $hour $minutes $minute

I seem to be suffering from a senior moment and can't figure out how to save the longest uptime and have it updated when the current uptime is greater then the longest uptime. I am going to write this to the MOTD when someone logs into the system.

raconteur 10-15-2008 12:40 PM

I think the simplest method would be to store the current max value in a text file (I'd probably select a dotfile but that is a personal choice) and compare it to the current value, overwriting the data in the text file if the current value is greater.

If you're asking about the details of the comparison, I'd suggest using eval or bash/csh builtin arithmetic comparisons. I'm sure myself or others could give you examples of that sort of thing if that's what you're after.

hth

unSpawn 10-15-2008 01:03 PM

Wouldn't just converting both values to epoch and subtracting be easier?

bgeddy 10-17-2008 05:00 AM

This just stores the higher numeric value of what's already stored or the first field of proc uptime. If nothing is there when the script is ran it will store the contents of field 1 of /proc/uptime. Note the file location /home/ed/.myuptime will need to be altered for your system.

I attempt no manipulation of the values simply storing the larger of the two. Any manipulation can be done by a display routine - something like your script.

Code:

#!/bin/bash
FLE="/home/ed/.myuptime"
if [[ -f "$FLE" ]]
then       
        OLD_UP=$(cat "$FLE")
else       
        OLD_UP=0
fi
NEW_UP=$(awk '{ print $1 }' /proc/uptime)
if [[  $(echo "$NEW_UP > $OLD_UP" | bc) -eq 0 ]]
then
        NEW_UP=$OLD_UP
fi
echo "$NEW_UP" > "$FLE"

Hope this is useful to you.

McSlack 10-18-2008 12:41 PM

Thanks! I was able to finish my script using your suggestions. I had to do a little reading to understand what was happening with the bash script and apply that logic it to csh.

Code:

if ( -e .maxuptime ) then
 if ( `cat .maxuptime ` < $systime ) then
    echo $systime > .maxuptime
 endif
  else
    echo $systime > .maxuptime
endif
set systime = `cat .maxuptime `


bgeddy 10-18-2008 02:13 PM

Quote:

I had to do a little reading to understand what was happening with the bash script and apply that logic it to csh.
Oops - I always assume everyone will be using bash - sorry ! Hope it wasn't too much work translating !


All times are GMT -5. The time now is 02:56 PM.