LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 04-18-2007, 03:56 PM   #1
SamuelHenderson
LQ Newbie
 
Registered: Sep 2006
Posts: 17

Rep: Reputation: 0
Formatting uptime


Hello everyone.

I am looking for a way to format *nix system uptimes into a machine friendly form.

Currently the uptime is displayed as:

.. up 1 day, 3:00, .. I need it to be in plain numerical form like 1620 (as in the number of seconds of uptime)

My first idea was just use a the ::hrSystemUptime.0 MIB but my boss stated not to use that. He wants me to toy around with formatting the uptime using the date command somehow..

Any suggestions?

Last edited by SamuelHenderson; 04-18-2007 at 04:08 PM.
 
Old 04-18-2007, 04:19 PM   #2
macemoneta
Senior Member
 
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
Blog Entries: 2

Rep: Reputation: 344Reputation: 344Reputation: 344Reputation: 344
cat /proc/uptime

The first field is the time since boot, the second is total idle time.
 
Old 04-18-2007, 06:31 PM   #3
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Quote:
cat /proc/uptime
I realize this is slightly off topic, but do you know of any online or other summary of what information exists in the /proc file system, and how to interpret it. I've looked around in the past, and never found any concise or complete explanation. Some stuff is fairly obvious, but a lot of it you can only guess what it means. I'm given to believe that it tends to change over time, and people are reluctant to document it for fear of locking in to something.

Thanks.
--- rod.
 
Old 04-18-2007, 06:43 PM   #4
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Quote:
Originally Posted by theNbomr
I realize this is slightly off topic, but do you know of any online or other summary of what information exists in the /proc file system, and how to interpret it. I've looked around in the past, and never found any concise or complete explanation. Some stuff is fairly obvious, but a lot of it you can only guess what it means. I'm given to believe that it tends to change over time, and people are reluctant to document it for fear of locking in to something.

Thanks.
--- rod.
The /proc filesystem is dependent on what kernel you use (linux vs. BSD vs. Solaris, etc.). For linux, rudimentary (and incomplete) /proc documentation can be found in the kernel source tree under Documentation/filesystems/proc.txt.
 
Old 04-19-2007, 08:58 AM   #5
SamuelHenderson
LQ Newbie
 
Registered: Sep 2006
Posts: 17

Original Poster
Rep: Reputation: 0
Quote:
I realize this is slightly off topic, but do you know of any online or other summary of what information exists in the /proc file system, and how to interpret it. I've looked around in the past, and never found any concise or complete explanation. Some stuff is fairly obvious, but a lot of it you can only guess what it means. I'm given to believe that it tends to change over time, and people are reluctant to document it for fear of locking in to something.
A coworker who has been using Linux much longer than I havwe said the /proc was full of kernel messages and stuff like that. I did a bit of research on this to find out for myself as well. I found the following http://www.tuxfiles.org/linuxhelp/linuxdir.html]here:

Quote:
This is a special directory. Well, actually /proc is just a virtual directory, because it doesn't exist at all! It contains some info about the kernel itself. There's a bunch of numbered entries that correspond to all processes running on the system, and there are also named entries that permit access to the current configuration of the system. Many of these entries can be viewed.
 
Old 04-19-2007, 09:20 AM   #6
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
You can't use date - convert the seconds of uptime to a broken down structure (struct tm) with gmtime() then call strftime() to format the result, which is what date uses - strftime honors the same format constants as does date.
 
Old 04-19-2007, 10:19 AM   #7
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
if you don't want to find through /proc, you can do a "poor man"'s one. like doing your own calculations,
Code:
#!/bin/sh
uptime| awk '$0 ~ /(days)/ { day=$3 ; total_day = day * 86400 ; 
                           gsub(/,/ ," ",$5)                 
                          split($5, array,  ":")
                          total = total_day + (array[1] * 3600 ) + array[2] 
                          }
                $0 !~ /days/ { 
                             gsub(/,/ ," ",$3 )
                             split($3, array,  ":")
                             total = (array[1] * 3600 ) + array[2] 
                          }
                          END { print "Total uptime in secs: "  total }'
 
Old 04-21-2007, 10:35 AM   #8
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 735

Rep: Reputation: 76
Hi.
Quote:
Originally Posted by jim mcnamara
You can't use date ...
To paraphrase the automobile ad of some years ago, "GNU/Linux date is not your father's date".

It looks like you can get seconds since the epoch. You can specify a specific date in place of the current date. Doing some arithmetic seems like it would provide an answer ... cheers, makyo
 
  


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: 2.4 vs 2.6 Synesthesia Linux - General 4 10-17-2005 03:23 PM
24/7 uptime? jollyjoice General 20 01-25-2005 11:12 AM
uptime for server einstien Linux - General 2 09-20-2004 08:35 PM
uptime/users Cichlid Linux - General 3 09-17-2003 11:07 PM
uptime skate Linux - Software 16 08-14-2003 03:10 AM

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

All times are GMT -5. The time now is 12:18 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