Python: Uptime script

I've been writing Python for a grand total of 2 days. I recently wrote a little Python script to print a Linx or UNIX system's uptime. This took about 10 minutes to write, so I was pretty pleased. It even runs well on a 75MHz Busybox embedded Linux system. Check it out:

PYTHON:
  1. #!/usr/bin/python
  2. import os
  3.  
  4. #----------------------------------------
  5. # Gives a human-readable uptime string
  6. def uptime():
  7.  
  8.      try:
  9.          f = open( "/proc/uptime" )
  10.          contents = f.read().split()
  11.          f.close()
  12.      except:
  13.         return "Cannot open uptime file: /proc/uptime"
  14.  
  15.      total_seconds = float(contents[0])
  16.  
  17.      # Helper vars:
  18.      MINUTE  = 60
  19.      HOUR    = MINUTE * 60
  20.      DAY     = HOUR * 24
  21.  
  22.      # Get the days, hours, etc:
  23.      days    = int( total_seconds / DAY )
  24.      hours   = int( ( total_seconds % DAY ) / HOUR )
  25.      minutes = int( ( total_seconds % HOUR ) / MINUTE )
  26.      seconds = int( total_seconds % MINUTE )
  27.  
  28.      # Build up the pretty string (like this: "N days, N hours, N minutes, N seconds")
  29.      string = ""
  30.      if days> 0:
  31.          string += str(days) + " " + (days == 1 and "day" or "days" ) + ", "
  32.      if len(string)> 0 or hours> 0:
  33.          string += str(hours) + " " + (hours == 1 and "hour" or "hours" ) + ", "
  34.      if len(string)> 0 or minutes> 0:
  35.          string += str(minutes) + " " + (minutes == 1 and "minute" or "minutes" ) + ", "
  36.      string += str(seconds) + " " + (seconds == 1 and "second" or "seconds" )
  37.  
  38.      return string;
  39.  
  40. print "The system uptime is:", uptime()

One interesting thing about this script is its emulation of the C ternary operator using Python's short-circuited "and" operator. Notice this bit:

PYTHON:
  1. (days == 1 and "day" or "days" )

Python doesn't have the C-style ternary operator:

C++:
  1. (days == 1 ? "day" : "days")

So the short-circuited "and" is a decent alternative. Instead of ignorantly printing "1 days", this code will actually print "1 day" and "2 days" like it should.

4 Responses to “Python: Uptime script”

  1. Jesse Says:

    Nice script! I am going to use it in a script I am writing for my backup server at home (open Source of course). You saved me a lot of time.

  2. Filip Says:

    Thanks for the nice hack with ternary operator, I was searching for it for a while now.
    The script is nice to, though you might mention that, in order to be able to call it “uptime” instead of “python uptime” a line “#!/path/to/python/interpreter” should be added (it also took me a while to figure this out in the beginning).

  3. Dave Says:

    This was meant as an example code snippet that others could use in their Python programs, not as a stand-alone script. For that purpose, I would just use the “uptime” command.

    –Dave

  4. Friedmud Says:

    Thanks for the idea… I was just getting ready to call “uptime” via ssh to get the load average on a bunch of machines…. but after seeing this script I noticed that all I have to do is something like:

    file = os.popen(’ssh machine_name “cat /proc/loadavg”‘)
    contents = file.read().split()
    print contents[0]

    Which is much easier!

    Friedmud

Leave a Reply