LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-15-2008, 11:27 AM   #1
McSlack
Member
 
Registered: Jul 2006
Location: Grand Rapids, MI
Distribution: Slackware
Posts: 44

Rep: Reputation: 15
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.
 
Old 10-15-2008, 12:40 PM   #2
raconteur
Member
 
Registered: Dec 2007
Location: Slightly left of center
Distribution: slackware
Posts: 276
Blog Entries: 2

Rep: Reputation: 44
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

Last edited by raconteur; 10-15-2008 at 12:41 PM. Reason: just now noticed you were using csh
 
Old 10-15-2008, 01:03 PM   #3
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Wouldn't just converting both values to epoch and subtracting be easier?
 
Old 10-17-2008, 05:00 AM   #4
bgeddy
Senior Member
 
Registered: Sep 2006
Location: Liverpool - England
Distribution: slackware64 13.37 and -current, Dragonfly BSD
Posts: 1,810

Rep: Reputation: 232Reputation: 232Reputation: 232
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.
 
Old 10-18-2008, 12:41 PM   #5
McSlack
Member
 
Registered: Jul 2006
Location: Grand Rapids, MI
Distribution: Slackware
Posts: 44

Original Poster
Rep: Reputation: 15
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 `
 
Old 10-18-2008, 02:13 PM   #6
bgeddy
Senior Member
 
Registered: Sep 2006
Location: Liverpool - England
Distribution: slackware64 13.37 and -current, Dragonfly BSD
Posts: 1,810

Rep: Reputation: 232Reputation: 232Reputation: 232
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 !
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
uptime sh script adnanm Linux - Newbie 1 04-20-2007 10:39 PM
Linux uptime script.. billeman Linux - Software 1 09-19-2005 11:11 AM
cups printing with a water mark ? michael_util Linux - General 1 02-10-2005 04:53 PM
Show Uptime On Shell Login~ skate Linux - Software 2 09-09-2003 12:48 PM
'uptime' bash script icyfire Linux - Software 4 01-10-2003 05:53 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:19 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration