Run as SystemD Service
Closed this issue · 4 comments
Hello,
thanks for mqtt-launcher!
I would like to share some knowledge on how I run it as systemd-service:
/etc/systemd/system/Smartlock.service
[Unit]
Description=Smartlock
Require = network-online.target
After = network-online.target
[Service]
Type=simple
Environment=MQTTLAUNCHERCONFIG=/usr/local/bin/launcher.conf
WorkingDirectory=/usr/local/bin
ExecStart=/usr/bin/python /usr/local/bin/mqtt-launcher.py
Restart=always
StandardInput=tty-force
StandardOutput=tty
TTYPath=/dev/tty11
[Install]
WantedBy=multi-user.target
Now this did not work, and I suspected, it was due to the problem, that the config-file was not found. thus, I hardcoded the config file in the /usr/local/bin/mqtt-launcher.py;
Not sure, this was neccessary though.
Then, the final problem seemed to be, that something (network?) was not ready yet. At least the service worked when launching it a while after boot, but it did not work when running during boot.
Thus, I changed mqtt-launcher.py:
#CONFIG=os.getenv('MQTTLAUNCHERCONFIG', 'launcher.conf')
CONFIG="/usr/local/bin/launcher.conf"
time.sleep(30)
With this, it is working.
Hope, this helps someone, and maybe someone can comment.
Gretings,
Hendrik
Thank you.
My solution works like a charm:
[Unit]
Description=MQTT launcher
After=multi-user.target
[Service]
Type=simple
WorkingDirectory=/data/mqtt-launcher
ExecStart=/usr/bin/python /data/mqtt-launcher/mqtt-launcher.py
Restart=always
[Install]
WantedBy=multi-user.target
Hello,
thanks for mqtt-launcher!
I would like to share some knowledge on how I run it as systemd-service:/etc/systemd/system/Smartlock.service [Unit] Description=Smartlock Require = network-online.target After = network-online.target [Service] Type=simple Environment=MQTTLAUNCHERCONFIG=/usr/local/bin/launcher.conf WorkingDirectory=/usr/local/bin ExecStart=/usr/bin/python /usr/local/bin/mqtt-launcher.py Restart=always StandardInput=tty-force StandardOutput=tty TTYPath=/dev/tty11 [Install] WantedBy=multi-user.target
Now this did not work, and I suspected, it was due to the problem, that the config-file was not found. thus, I hardcoded the config file in the /usr/local/bin/mqtt-launcher.py;
Not sure, this was neccessary though.Then, the final problem seemed to be, that something (network?) was not ready yet. At least the service worked when launching it a while after boot, but it did not work when running during boot.
Thus, I changed mqtt-launcher.py:
#CONFIG=os.getenv('MQTTLAUNCHERCONFIG', 'launcher.conf') CONFIG="/usr/local/bin/launcher.conf" time.sleep(30)
With this, it is working.
Hope, this helps someone, and maybe someone can comment.
Gretings,
Hendrik
You almost got it right. You can either use WorkingDirectory so systemd changes to that dir and then loads the config no probs, or if you want to use the Environment the you just need to put the export string in the quotes.
Environment="MQTTLAUNCHERCONFIG=/usr/local/bin/launcher.conf"
Best,
TBP
For me under Ubuntu 20.04 the service start command was hanging until I removed the After=
parameter:
[Unit]
Description=MQTT launcher
[Service]
Type=simple
User=root
WorkingDirectory=/usr/local/bin/mqtt
ExecStart=/usr/bin/python /usr/local/bin/mqtt/mqtt-launcher.py
Restart=always
[Install]
WantedBy=multi-user.target