gpbenton / engMQTTClient

MQTT client for Energenie ener314-rt board running on a raspberry Pi
MIT License
21 stars 10 forks source link

Add systemd service example files #18

Closed davet2001 closed 3 years ago

davet2001 commented 4 years ago

It makes sense to run this as as a systemctl type service, so that you can do systemctl start engmqttclient.service etc. A sample systemd service file would help with this and make installation easier.

I have a work in progress version of this already.

gpbenton commented 4 years ago

I just have this line in /etc/rc.local LD_LIBRARY_PATH=/usr/local/lib /home/pi/engMQTTClient/engMQTTClient &

I haven't had the need to do anything else.

davet2001 commented 4 years ago

I agree, this works. Also could add to crontab @reboot which is what I useed initially.

But when debugging problems it is common to need to restart it, identify if it is still running and look at the log files. In addition engMQTTclient must only start up when the network is ready. If the MQTT broker is running locally, engMQTTclient must start after the broker.

systemctl and journalctl handle all of these requirements cleanly, and would be one step closer towards a distributable/installable package.

My feeling was to add a sample file in case someone wanted to use it, possibly defining the server details in a separate file such as /etc/engMQTTclient.conf

bwims commented 4 years ago

I would like a systemctl method as my Rpi uses its own dedicated broker, but am not linux-competent enough to make my own. Davet2001, would you be so kind as to announce when you have succeeded? Thanks!

gpbenton commented 4 years ago

Adding a configuration file is issue #3. If you can do that for me it would be great, but I think you could include everything necessary in the systemd service definition itself, so it isn't really necessary.

I am surprised you say that engMQTTClient needs to start up after the network and broker. AFAIK, it will fail to connect and then retry under either circumstance.

It is very difficult to determine from SystemV if the network is active - the After=network.service only indicates the network services have started, not that there is an active network, so I suspect that if there is a problem, it won't be solved by Systemd. I also seem to remember the systemd docs recommend that programs are well behaved without a network rather than relying on it to be there.

But of course if you come up with something that looks good, I'll quite happily include it in the repo.

davet2001 commented 4 years ago

I have created a PR. @bwims it would be great if you could test it out!

davet2001 commented 4 years ago

@gpbenton It turns out that systemd config files don’t like lots of things on the path they are calling. They expect to see a reference to just 1 binary or script. For that reason I created an intermediate script that includes the config file and makes the call to launch engMQTTClient.

ghost commented 4 years ago

@gpbenton I have attached a systemd service unit file for your MQTT client and a service unit file for openhab2 that requires your MQTT client. Both these files should be copied to /etc/systemd/system directory under linux. Tested and working in Rapsbian and Debian.

This config expects the engMQTTClient executable to be in /usr/local/bin , i.e.: create a symlink to the build directory using

# ln -s <-MQTTClient-executable-path> /usr/local/bin/enqMQTTClient
# ls -l /usr/local/bin/engMQTTClient
lrwxrwxrwx 1 root root 61 Jan 14 13:31 /usr/local/bin/engMQTTClient -> /home/openhabian/Energenie/engMQTTClient-master/engMQTTClient

NOTE This setup assumes you have installed the liblog4c and other library files required by the client. If you have build from source then you should know how to install the libs from your build directory !!

systemd_services.zip

gpbenton commented 4 years ago

@tillykeats Thanks for this, it looks much as I would expect, so I have added it as is to a subdirectory. Can you check a couple of things though 1) There are a couple of user definitions in the engMQTTClient services file, and I'm not sure whether systemd usess the first or last, so can you remove the one that isn't used. 2) I don't really believe that engMQTTClient is required for OpenHab to start - they may be running on different machines. So I think you can remove that from the requires line. At which point I think this is just a generic openhab2 script?

bwims commented 4 years ago

@tillykeats Would it also be possible to comment lines that can be removed if you are running standalone and do not have an openhab installation? I'd be really grateful!

ghost commented 4 years ago

Please accept my apologies. I have been working in Saudi Arabia for 6 months and recently lost internet access. I can probably only state - to retain my VISA! - that the internet here is not as "corrupted" as in the West ! I note a pull request using @davet2001 code so that overtakes my support. On that subject, I agree with Graham that a file based config with one or two env vars is the appropriate method , as opposed to having multiple config env vars - that is inflexible for example if moving from SystemV type daemons, or even in some cases staying within SystemV but using a different linux flavour. Just my tuppenceworth.

gpbenton commented 3 years ago

Nothing happened for 6 months, so closing

davet2001 commented 3 years ago

Sorry for not progressing this.

I ended up bypassing MQTT, engmqttclient and the ener-314 entirely, running a direct 433MHz transmitter on my Homeassistant server. It doesnt need any additional service or installation at all.