Mqtt_input script run as systemd instead of initd

Apologies, I missed your post above. Got it now, thanks.

Is possible I would like to move to using the systemd journal log, this seems to be the preferred way to go ‘all-in’ with systemd. This is how nodeRED runs. I have been looking at the nodeRED unit file as an example.

However, it would be good to give the option for users to log to mqtt_input.log if they prefer as a commented out option in the unit file which can easily be enabled.

I have updated the unit file to use @Andy_Taylor logging code as an alternative commented out option. I don’t quite understand what is meant by forking. I will leaf type=forking commented out.

from: [Solved] systemd - To fork or not to fork when writing services / Applications & Desktop Environments / Arch Linux Forums

If you have a choice then use Type=simple for your systemd services. It is simpler and systemd will take care or proper process deamonizing, restarts and shutdowns.

Type=forking is for historical services that cannot be run in foreground. In this case process itself will take care of its deamonizing, but systemd should know the child pid somehow so you need to provide PIDFile= option as well.

I choose type=idle to ensure the script starts after all other services have started. Here is a full explanation of the different service types:

https://wiki.archlinux.org/index.php/Systemd#Service_types

I will keep these changes in the master branch until they have been more tested:

Yes adding -o short instead of -o cat will display the date

sudo journalctl -f -u mqtt_input -o short
-- Logs begin at Fri 2017-01-06 08:35:09 UTC. --
Jan 06 13:44:13 emonpi mqtt_input[18518]: emon/emonpi/t5 0
Jan 06 13:44:13 emonpi mqtt_input[18518]: emon/emonpi/t6 0
Jan 06 13:44:13 emonpi mqtt_input[18518]: emon/emonpi/pulsecount 1032
Jan 06 13:44:13 emonpi mqtt_input[18518]: emon/emonpi/rssi 0
Jan 06 13:44:13 emonpi mqtt_input[18518]: emon/emonth5/temperature 13
Jan 06 13:44:13 emonpi mqtt_input[18518]: emon/emonth5/external temperature 13
Jan 06 13:44:13 emonpi mqtt_input[18518]: emon/emonth5/humidity 64.9
Jan 06 13:44:13 emonpi mqtt_input[18518]: emon/emonth5/battery 2.7
Jan 06 13:44:13 emonpi mqtt_input[18518]: emon/emonth5/pulsecount 1
Jan 06 13:44:13 emonpi mqtt_input[18518]: emon/emonth5/rssi -67
Jan 06 13:44:18 emonpi mqtt_input[18518]: emon/emonpi/power1 40
Jan 06 13:44:18 emonpi mqtt_input[18518]: emon/emonpi/power2 13
Jan 06 13:44:18 emonpi mqtt_input[18518]: emon/emonpi/power1pluspower2 53

There are lots of -o output options:

short, short-iso, short-precise, short-monotonic, verbose,export, json, json-pretty, json-sse, cat

I’ve just tested again on a fresh emonPi and the update script worked. Could it be that you have un-committed changes to either ~/emonpi or /var/www/emoncms folders on your pi? Try git pulling these folder manually.

Yes the update script does a reload:

Glyn - anything is possible - my Pi gets quite messed with while I am working on things - I’ll re-image it in the next few days and try it again - if your update works - its almost certainly an issue with mine.

I will also take a look at the log mqtt logs before / after the update and see if I can bottom that out.

1 Like

Having played with this a bit this afternoon, I have discovered the “Type=forking” is required for Andy’s code to work, however I have also figured out that the mqtt_script is both “logging” and “echoing” messages and the messages we are trying to redirect are std out not “log messages” as I expected. eg

I have found that the actual “log messages” are directed to emoncms.log if the loglevel is set correctly in settings.php and that the mqtt_input.log is superfluous.

This is why there is no timestamp when redirected.

I have run though phpmqtt_input.php and commented out most of (but not all) of the echo lines and returned the unit to it’s standard form and now the logging works as expected.

1 Like

Just deleted my init.d script to start mqtt_input, and got the following results on a ‘self-build’ emoncms (up to date with git).

sudo cp /var/www/emoncms/scripts/mqtt_input.service /etc/systemd/system/mqtt_input.service
sudo systemctl daemon-reload
…everythink ok so far, but when I try and enable the service;
sudo systemctl enable mqtt_service
I get;
Failed to execute operation: No such file or directory

If I look in the /etc/systemd/system directory, I can see the mqtt_input.service file is present.

Checking the log, I see;
Loaded: loaded (/etc/systemd/system/mqtt_input.service; disabled) Active: inactive (dead)

I can manually start the service OK;
sudo systemctl start mqtt_input

…but as the service is not enabled, it won’t restart after a reboot.

Paul

That should be sudo systemctl enable mqtt_input.service

1 Like

Cheers Paul. I’ve amended the command on the script fie, do you want to add this to your read.me git PR?

Paul

I have corrected the typo in the readme.md, but I think that section needs a re-write and the old instructions resurrecting as per my comment on the commit.

1 Like

I’ve done a quick re-write to add back the section which I wrote in Oct 2015, which uses init.d instead of systemd as an alternative solution for older OS’s.
…For consideration.

Paul

Thanks a lot guys, the PR’s fixes and doc updates have been tested and merged (into master & stable). :thumbsup:

6 posts were split to a new topic: Unable to use certain symbols in MQTT topics with emonCMS mqtt_input

4 posts were split to a new topic: Installing Mosquitto for emoncms