Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run as SystemD Service #20

Closed
henfri opened this issue Jun 24, 2019 · 4 comments
Closed

Run as SystemD Service #20

henfri opened this issue Jun 24, 2019 · 4 comments

Comments

@henfri
Copy link

henfri commented Jun 24, 2019

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

@jpmens
Copy link
Owner

jpmens commented Jun 25, 2019

Thank you.

@turbulator
Copy link

turbulator commented Dec 2, 2019

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

@TheBluProject
Copy link

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

@nagyrobi
Copy link

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

@jpmens jpmens closed this as completed Dec 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants