uptime in unix
Fredrik Lundh
fredrik at pythonware.com
Sun Sep 19 16:33:46 EDT 2004
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>
More information about the Python-list
mailing list