uptime in unix

O

Oli Schwarz

Hello,

how can I read out the uptime of a unix system in python (Linux and *BSD).

I have not found a uptime-function in the Library.

Regards
Oli
 
N

none

Oli said:
Hello,

how can I read out the uptime of a unix system in python (Linux and *BSD).

I have not found a uptime-function in the Library.

Regards
Oli

You can call the uptime program from python and catch the ouput:

21:29:57 up 9:35, 6 users, load average: 1.33, 2.04, 2.08
 
F

Fredrik Lundh

Oli said:
how can I read out the uptime of a unix system in python (Linux and *BSD).

I have not found a uptime-function in the Library.

try this:

uptime, idletime = [float(field) for field in open("/proc/uptime").read().split()]

(times in seconds)

I don't know if this works on *BSD; in worst case, you have to parse the output
from the uptime command itself:

import re, os

output = os.popen("uptime").read()

m = re.search("up (\d+) days,\s+(\d+):(\d+)", output)
if not m:
raise ValueError("unknown uptime format")

days, hours, minutes = map(float, m.groups())

uptime = days * 86400 + hours * 3600 + minutes * 60

(tweak as necessary)

</F>
 
C

Cameron Laird

Hello,

how can I read out the uptime of a unix system in python (Linux and *BSD).

I have not found a uptime-function in the Library.
.
.
.
Is it enough to parse the output of uptime(1)?
 
O

Oli Schwarz

Cameron Laird said:
.
.
.
Is it enough to parse the output of uptime(1)?

I hope. :) But I was wondering that there is no such function in the
library.

Are these things handled like in Java? Doesn't Python know anything
about such platform specific things?

Regards
Oli
 
M

marcus

Oli said:
Hello,

how can I read out the uptime of a unix system in python (Linux and *BSD).

I have not found a uptime-function in the Library.

Regards
Oli
You can pull the uptime in seconds from /proc/uptime in Linux (use
uptime in BSD, prob. Darwin).
How you retrieve it is a matter of personal taste..


For instance:

import commands
#linux
commands.getoutput('cat /proc/uptime')
etc..


Obviously if you're going to use popen, you could use the unix uptime
command anyway I guess.

I'm guessing that's not what you want though, since it's just using a
shell..
 
D

Dennis Lee Bieber

Are these things handled like in Java? Doesn't Python know anything
about such platform specific things?
Typically only in a platform specific import module...

For the most part, Python tries to stay platform neutral.

--
 
C

Cameron Laird

I hope. :) But I was wondering that there is no such function in the
library.

Are these things handled like in Java? Doesn't Python know anything
about such platform specific things?
.
.
.
There's no particularly satisfying abstract answer to these
matters. History happened, and we live in the result. Python
knows about some platform-specific items, and not others. I'm
unconvinced that Java's truths are any more rationalized.
 
H

Heiko Wundram

Am Sonntag, 19. September 2004 23:21 schrieb Oli Schwarz:
Are these things handled like in Java? Doesn't Python know anything
about such platform specific things?

Python knows a lot about platform specific things (look at five different
implementations of os.path), or different implementations of mmap, which
works completely different on Unix than on Windows.

But, Python abstracts those differences away, and presents a coherent
interface to all this functionality. There are only very few function which
are actually platform specific.

Uptime and the like (for example, list of loaded kernel modules is another
candidate) is something that's not only platform specific, but also very
operating-system specific. Take Linux: it "invented" the /proc filesystem
(well, actually not the filesystem, but the hierarchy as it is know) to
present this kind of system info from the kernel, so open file, read, close
file. Take *BSD: they have /proc, but it's only present when running in Linux
compatability mode. Standard BSD has a SYSCALL to retrieve uptime using some
form of kernel config management with read only keys. Take Solaris: uptime is
retrieved using a SYSCALL, but this is a specific SYSCALL rather than some
config subsystem syscall as with BSD.

And Windows? "What's Uptime?" :)

So, I guess uptime is just a concept that's so wildly different among
different operating systems (although the platform may be the same, for *nix
it's Posix, pretty much) that it's simply better not to add something like
this to the std-lib (I guess Pythonistas once reasoned like this, and I can
agree fully), but rather let the programmer do the job.

And, oh, if you thought that parsing the output of /usr/bin/uptime would solve
your problems: no, it only creates more, because the output of this command
isn't standardized either. With Linux you get the GNU version (well most of
the time, but not necessarily either), whose output is slightly different
than what the Posix standard specifies, whose output is slightly different
than the *BSD versions (who have a longer history and remain backwards
compatible), whose output is slightly different to the SysV version of uptime
(which needed to be different to *BSD in the 80s to bind users, so it was
made different), whose output... You name it... :)

Heiko.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top