Run script as a systemd daemon

To create own systemd service we need to create an unit file in a directory /etc/systemd/system/ e. g. mqtt2web.service with a content like this:

[Unit]
Description=Forwards MQTT messages to REST API
After=network.target

[Service]
ExecStart=python3 /opt/mqtt2web/mqtt_watcher.py
User=root
Restart=always
RestartSec=10s

[Install]
WantedBy=multi-user.target

To start the service:

systemctl start mqtt2web

And to automatically start after a boot:

systemctl enable mqtt2web