473,385 Members | 1,528 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

uptime in unix

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
Jul 18 '05 #1
8 5239
Oli Schwarz wrote:
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>

Jul 18 '05 #2
Oli Schwarz wrote:
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:

import os
print os.popen('uptime').read()

21:29:57 up 9:35, 6 users, load average: 1.33, 2.04, 2.08
Jul 18 '05 #3
In article <ci**********@online.de>, Oli Schwarz <ho***@gmx.net> wrote:
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.

Jul 18 '05 #4
Cameron Laird <cl****@lairds.us> schrieb:
In article <ci**********@online.de>, Oli Schwarz <ho***@gmx.net> wrote:
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)?


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
Jul 18 '05 #5
Oli Schwarz wrote:
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..
Jul 18 '05 #6
On Sun, 19 Sep 2004 21:21:36 +0000 (UTC), Oli Schwarz <ho***@gmx.net>
declaimed the following in comp.lang.python:

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.

-- ================================================== ============ <
wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
================================================== ============ <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.netcom.com/> <

Jul 18 '05 #7
In article <ci**********@online.de>, Oli Schwarz <ho***@gmx.net> wrote:
.
.
.
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?

Jul 18 '05 #8
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.
Jul 18 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

24
by: Esmail Bonakdarian | last post by:
Hi, Is there a way to display how long a Win XP system has been up? Somewhat analogous to the *nix uptime command. Thanks, Esmail
6
by: MA | last post by:
Hi all! I know there is other newsgroups for webservices, but I donīt get any answers on those. I have developed a webservice that writeing and reading files in different folders. Question...
4
by: Brian Henry | last post by:
does anyone know of an easy way to say how long has a specific system been up and running? I just need to get back the uptime of the servers on our network for a report, they are all in a Active...
4
by: Dylan Parry | last post by:
Hi, I'm looking for a way to calculate the amount of time that Windows has been running since the last boot. Ideally I'd like to be able to create a DateTime object containing this value so that...
0
by: streamkid | last post by:
hello.. could anyone please help me build a uptime script for openbsd? i tried phpsysinfo, it doesn't work (only uptime works, and that's around 15k days :p) i tried these: <?php...
8
by: Guy Macon | last post by:
How to increase web server uptime with DNS failover: For illustration, let's start with some really bad hosting... Find four free web hosts that are each 90% reliable -- in other words they...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.