Seite 1 von 1

Upload MQTT Free Space Information

Verfasst: Sonntag 2. August 2020, 09:22
von zeron850
Hallo Zusammen,

ich habe folges Skript erstellt damit dieses den Upload des freien Speichers der SD Card zu einem MQTT Broker durchführt:

Code: Alles auswählen

[#!/usr/bin/env python3
import paho.mqtt.publish as publish
from subprocess import check_output
from re import findall

def get_space():
    space = check_output(df -P / | tr -s " " " " | cut -d " " -f 4).decode("UTF-8")
    return(findall("\d+\.\d+",space)[0])

def publish_message(topic, message):
    print("Publishing to MQTT topic: " + topic)
    print("Message: " + message)

    publish.single(topic, message, hostname="192.168.1.31")

temp = get_space()
publish_message("Home/pi/Space", space)


Allerdings erhalte ich folgende Fehlermeldung:

df: Ungültige Option --
„df --help“ liefert weitere Informationen.
Traceback (most recent call last):
File "/home/pi/space.py", line 16, in <module>
temp = get_space()
File "/home/pi/space.py", line 7, in get_space
space = check_output(["df","-P / | tr -s " " " " | cut -d " " -f 4"]).decode("UTF-8")
File "/usr/lib/python3.7/subprocess.py", line 395, in check_output
**kwargs).stdout
File "/usr/lib/python3.7/subprocess.py", line 487, in run
output=stdout, stderr=stderr)
subprocess.CalledProcessError: Command '['df', '-P / | tr -s | cut -d -f 4']' returned non-zero exit status 1.


Nomral lauter der Befehl zur Abfrage:

Code: Alles auswählen

 df -P / | tr -s " " " " | cut -d " " -f 4 

An was könntes es liegen?

Danke & Grüße
Thomas

Re: Upload MQTT Free Space Information

Verfasst: Sonntag 2. August 2020, 09:27
von __deets__
Subprocess kann keine vollen Shell-Kommandos ausfuehren. Installier dir einfach das psutil-Modul, und du kannst auf diese Werte direkt zugreifen.

Re: Upload MQTT Free Space Information

Verfasst: Sonntag 2. August 2020, 09:31
von Sirius3
Der Code in der Fehlermeldung weicht von gezeigten ab. In check_output kann man nicht einfach irgendwelche Shell-Befehle übergeben. Es wäre auch deutlich lesbar, das Parsen des Outputs in Python zu schreiben. Jedes Argument muss ein eigenes Listenelement sein

Code: Alles auswählen

check_output(["df","-P", "/"])

Re: Upload MQTT Free Space Information

Verfasst: Sonntag 2. August 2020, 09:38
von zeron850
Ok, Danke für eure Hilfe!