0

This question refers to: uptime with Python

If I use the programm on a raspberry, I get the wrong time:

~ $ uname -a
Linux raspberry 5.4.72-v7+ #1356 SMP Thu Oct 22 13:56:54 BST 2020 armv7l GNU/Linux


pi@raspberry:~ $ uptime
15:14:33 up 20 min,  3 users,  load average: 0,00, 0,08, 0,09

But the output is:

20 0 1 # days, hours, minutes

I'm using:

import subprocess 
def uptime1(): # liefert Zeit in s
    raw = subprocess.check_output('uptime').decode("utf8").replace(',', '')
    days = int(raw.split()[2])
    if 'min' in raw:
        hours = 0
        minutes = int(raw[4])
    else:
        hours, minutes = map(int,raw.split()[4].split(':'))
    print(days, hours, minutes)    
    totalsecs = ((days * 24 + hours) * 60 + minutes) * 60
    return totalsecs

zeit=uptime1()

How can that be solved?

Speedy-10
  • 11
  • 3

4 Answers4

2

I think your logic for parsing the results of uptime needs work. If you want to stick with your way, you should print out each of the substrings are that you're pulling from the output to see if you have what you think you have. I think you'll find that you need to tweak your code quite a bit. To get you started on that, here's what the result of splitting the output gives you:

['15:14:33', 'up', '20', 'min,', '3', 'users,', 'load', 'average:', '0,00,', '0,08,', '0,09']

You're using [2] and [4] from this split, which is minutes and number of users, but you seem to be expecting the first to give you days and the second to give you minutes.

As a suggestion of another way, here's code that will simply pull all of the numbers out of a string, which would give you a starting point for evaluating all the data in the output:

import re

output = '15:14:33 up 20 min,  3 users,  load average: 0,00, 0,08, 0,09'

exp = re.compile(r"(\d+)")

i = 0
r = []

while True:
    m = exp.search(output, i)
    if not m:
        break
    r.append(int(m.group(1)))
    i = m.end(1)

print(r)

Result:

[15, 14, 33, 20, 3, 0, 0, 0, 8, 0, 9]

So from here, you can just do r[3], r[5], etc. to get at all the numbers. This is just one way you might parse the output.

CryptoFool
  • 21,719
  • 5
  • 26
  • 44
1

Try to set TimeZone to the correct for you:

import os
os.environ['TZ']="America/New_York"
time.tzset()

As described here Launching python within python and timezone issue. Subprosess is starting a process from Python context, and this can be different than your system.

Punnerud
  • 7,195
  • 2
  • 54
  • 44
0

import os os.environ['TZ']="America/New_York" time.tzset()

I got an error:

~ $ python3 /home/pi/melder_status.py 
Traceback (most recent call last):
  File "/home/pi/melder_status.py", line 11, in <module>
    time.tzset()
NameError: name 'time' is not defined
Speedy-10
  • 11
  • 3
0

I found another solution: https://www.raspberrypi.org/forums/viewtopic.php?t=164276

#!/usr/bin/python3

import shlex, subprocess
cmd = "uptime -p"
args = shlex.split(cmd)
p = subprocess.Popen(args, stdout=subprocess.PIPE)
output = p.communicate()

Thanks for your posted ideas. print (output)

Speedy-10
  • 11
  • 3