LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Formatting uptime (https://www.linuxquestions.org/questions/programming-9/formatting-uptime-547128/)

SamuelHenderson 04-18-2007 03:56 PM

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?

macemoneta 04-18-2007 04:19 PM

cat /proc/uptime

The first field is the time since boot, the second is total idle time.

theNbomr 04-18-2007 06:31 PM

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.

osor 04-18-2007 06:43 PM

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.

SamuelHenderson 04-19-2007 08:58 AM

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.

jim mcnamara 04-19-2007 09:20 AM

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.

ghostdog74 04-19-2007 10:19 AM

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 }'


makyo 04-21-2007 10:35 AM

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


All times are GMT -5. The time now is 10:50 AM.