Zum Inhalt springen

Python MQTT System Restart und Shutdown Script

Aus LHlab Wiki
Warnung Work in Progress

Diese Seite wird erst noch aufgebaut. Bitte noch keine Änderungen vornehmen, ohne mit dem ursprünglichen Author zu sprechen!
Vielen Dank

Dieses Python Script startet via MQTT Publish ein Linux System neu oder fährt es herunter. Das Ganze ist ideal in Kombination mit dem Raspberry Pi, einer openHAB Installation und einem Touchdisplay nutzbar. In meinem Szenario kann ich so via HABPanel entweder das unterliegende oder ein anderes Linux System Neustarten oder gar ganz ausschalten. HABPanel sendet hier den "reboot" oder "shutdown" Befehl über ein openHAB Item an den MQTT Broker. Das dortige Topic ist in einem Python Script, welches als System Daemon läuft abonniert und führt dann entsprechend einen reboot oder ein halt aus.

Voraussetzungen

Python restart_shutdown-mqtt.py

hier nun das Script, welches via MQTT Topic Payload einen Neustart oder einen Shutdown des Systems ausführt. Am besten legt ihr es unter /usr/local/sbin ab.

cd /usr/local/sbin

restart_shutdown-mqtt.py

#!/usr/bin/python3
import os, systemd.daemon, time
import paho.mqtt.client as mqtt, ssl

### set the variables
# MQTT Config
broker = "FQDN / IP ADDRESS"
port = 8883
publish_topic="home/attic/office"
clientid = "client-power"
hostname = "clientname"
username = "mosquitto"
password = "password"
insecure = True
qos = 1
retain_message = True
# Retry to connect to mqtt broker
mqttretry = 5

### do the stuff
print('Starting up Restart & Shutdown MQTT Service ...')

# just give some used variables an initial value
powerswitch = "Null"

### Functions
def publish(topic, payload):
  client.publish(publish_topic + "/" + topic,payload,qos,retain_message)

def on_connect(client, userdata, flags, rc):
  print("MQTT Connection established, Returned code=",rc)
  client.subscribe(publish_topic + "/" + hostname + "/system_power_switch", qos)

def on_message(client, userdata, message):
  global powerswitch
  if "system_power_switch" in message.topic:
    #print("MQTT system power payload:", str(message.payload.decode("utf-8")))
    powerswitch = str(message.payload.decode("utf-8"))

#MQTT Connection
mqttattempts = 0
while mqttattempts < mqttretry:
  try:
    client=mqtt.Client(clientid)
    client.username_pw_set(username, password)
    client.tls_set(cert_reqs=ssl.CERT_NONE) #no client certificate needed
    client.tls_insecure_set(insecure)
    client.connect(broker, port)
    client.loop_start()
    mqttattempts = mqttretry
  except :
    print("Could not establish MQTT Connection! Try again " + str(mqttretry - mqttattempts) + "x times")
    mqttattempts += 1
    if mqttattempts == mqttretry:
      print("Could not connect to MQTT Broker! exit...")
      exit (0)
    time.sleep(5)

# MQTT Subscription
client.on_message = on_message
client.on_connect = on_connect

# Tell systemd that our service is ready
systemd.daemon.notify('READY=1')

# finaly the loop
while True:
  try:
    #print("powerswitch Loop wert: ", powerswitch)
    if powerswitch == "shutdown":
      publish(hostname + "/system_power_switch", "on")
      print("System Shutdown")
      os.system('halt')
    elif powerswitch == "reboot":
      publish(hostname + "/system_power_switch", "on")
      print("System Reboot")
      os.system('reboot')
    time.sleep(1)

  except KeyboardInterrupt:
    print("Goodbye!")
    # At least close MQTT Connection
    client.disconnect()
    client.loop_stop()
    exit (0)

  except :
    print("An Error accured ... ")
    time.sleep(3)
    continue

# At least close MQTT Connection
client.disconnect()
client.loop_stop()

bitte passt die Variablen oben im Script an eure Bedürfnisse an!

Nun geben wir dem Script noch das Execute Recht:

chmod +x restart_shutdown-mqtt.py

einen ersten Test einfach durch das Aufrufen des Scripts machen.

restart_shutdown-mqtt.py

mit STRG-C könnt ihr es beenden.

System Service

Um das Ganze nun noch als tätig laufenden Service zu haben, erstellen wir ein Systemd Service file und aktivieren dieses. Hierzu legen wir als erstes das Service File an:

/etc/systemd/system/restart_shutdown.service

# systemd unit file for the restart_shutdown-mqtt Python Script
[Unit]

# Human readable name of the unit
Description=Python MQTT System Restart and Shutdown Service

# Starting after System is online and docker is running
# Only needed if MQTT is used
Wants=network-online.target
After=network-online.target
# Only needed if MQTT Broker is running in a Docker Container on the same Host
#After=docker.service
#After=docker.socket

[Service]

# Command to execute when the service is started
ExecStart=/usr/bin/python3 /usr/local/sbin/restart_shutdown-mqtt.py

# Disable Python's buffering of STDOUT and STDERR, so that output from the
# service shows up immediately in systemd's logs
Environment=PYTHONUNBUFFERED=1

# Automatically restart the service if it crashes
Restart=on-failure

# Our service will notify systemd once it is up and running
Type=notify

# Use a dedicated user to run our service
User=root


[Install]

# Tell systemd to automatically start this service when the system boots
# (assuming the service is enabled)
WantedBy=default.target

Bitte prüft hier den Part mit Wants und After falls der MQTT Broker im Docker Container läuft.

nun aktivieren wir den Service, damit er auch beim Neustart gestartet wird:

systemd daemon-reload
systemctl enable restart_shutdown.service

einen ersten Start können wir so ausführen:

systemctl start restart_shutdown.service

der Status des Skriptes sollte dann wie folgt aussehen:

systemctl status restart_shutdown.service

Restart script status

nun kann man das System einmal durchstarten und schauen ob der Service auch beim Reboot hochgefahren wird.

Installation via GIT Repository

Hier findet ihr das Projekt in GitHub:

Die Installation via Git erfolgt so:

# Installation
cd /usr/src
git clone https://github.com/alaub81/restart_shutdown-mqtt.git
cp restart_shutdown-mqtt/restart_shutdown-mqtt.py /usr/local/sbin/
cp restart_shutdown-mqtt/restart_shutdown.service /etc/systemd/system/
chmod +x /usr/local/sbin/restart_shutdown-mqtt.py
# Editieren der Dateien
nano /etc/systemd/system/restart_shutdown.service
nano /usr/local/sbin/restart_shutdown-mqtt.py
# Aktivieren des Autostart
systemctl enable restart_shutdown.service
systemctl start restart_shutdown.service

openHAB / HABPanel Anbindung