Community
OpenEnergyMonitor

Community

Development: Feedwriter systemd service


(Trystan Lea) #1

Following from @borpin’s maintenance mode thread, I’ve added a systemd service script for feedwriter (same as method used for phpmqtt_input), which works a treat for automatic restarting of feedwriter. This will make its way into an emonpi/emonbase update soon.

Removal of existing /etc/init.d feedwriter service:

sudo service feedwriter stop
sudo update-rc.d feedwriter disable
sudo rm /etc/init.d/feedwriter

Installation of systemd feedwriter service:

sudo ln -s /var/www/emoncms/scripts/services/feedwriter/feedwriter.service /lib/systemd/system
sudo systemctl enable feedwriter.service
sudo systemctl start feedwriter.service

To check the status of feedwriter:

sudo systemctl status feedwriter.service

Edited: for consistency with service-runner installation path.


(Brian Orpin) #2

Whilst I think this is a great idea, can we add in some consistency with other emoncms related services please.

@Greebo created a folder for service files in the scripts folder. https://github.com/emoncms/emoncms/tree/master/scripts/services/ with the intention of adding all services here eventually.

There was also a decision to use the /lib/systemd/system folder for the service file and link to it from there.

sudo ln -s /var/www/emoncms/scripts/services/service-runner/service-runner.service /lib/systemd/system

A daemon-reload command is not necessary at this point (as the daemon is not enabled). the enable command does everything necessary.

Have the general installation instructions been updated?


(Trystan Lea) #3

Yes happy with that. No instructions updated yet, just initial creation of systemd service script, with the intention of clarifying installation etc with @glyn.hudson next week.


(Trystan Lea) #4

I’ve moved the service file to services, but not the feedwriter.php script for now as that would likely be a breaking change. I’ve also updated the service file to mirror @Greebo’s service file as close as applicable:


(Paul Reed) #5

Are there any manual steps required to change an existing installation to use the changes made to the feedwriter & service-runner? Not everyone will be using a pre-configured emonpi image file.

If so, could the readme’s please be edited include the steps necessary.

Paul


(Trystan Lea) #6

Perhaps I should have noted that the above post was a development thread rather than a launch of a finished item. I will amend the title. Would appreciate help with readme’s for non pre-configured emonpi installs. Will be discussing this with Glyn this coming week as to emonpi steps.


(Paul Reed) #7

That is surely a responsibility of whoever introduced the changes, as they are most aware of the implications?
I’m not aware myself, so not really able to help in this instance.

Paul


(Brian Orpin) #8

I have not got it installed as I understand it was predominately to reduce SD card wear.

The only thing of note I can think of is the location of the core. Was emonpi core at /var/www/html/emoncms?


(Trystan Lea) #9

Are the steps I outlined above (with your recommended amendment @borpin) sufficient for self-build installs? It worked fine for me, disabling the existing script and installing the new service?

At the moment installation of feedwriter is documented as part of the low-write installation guide here:
https://github.com/emoncms/emoncms/blob/master/docs/RaspberryPi/Low-write-mode.md
If we replace the steps there with:

sudo ln -s /var/www/emoncms/scripts/services/feedwriter/feedwriter.service /lib/systemd/system
sudo systemctl daemon-reload
sudo systemctl enable feedwriter.service
sudo service feedwriter start

and then include the above steps for removal of the old service in the applicable release note, would that be sufficient?


(Brian Orpin) #10

Strictly (and for consistency), the command to start should be

sudo systemctl start feedwriter.service

and I would add in a

sudo systemctl status feedwriter.service

and a note on what the user should see (i.e. running) with this command.

I will say it yet again, the daemon-reload is not required and does nothing as, until you use the enable command, systemctl does not know the service exists (i.e. the enable loads it into the list of units to start; daemon-reload tells the system to check all the enabled units are loaded correctly by reloading them).

I cannot comment on what steps might be needed to switch from the cron based to systemd based running mechanism as I have never installed it.


(Trystan Lea) #11

Great thanks @borpin, I’ve amended the above as you recommend. We will update the documentation when this is properly released.


(Greebo) #12

sudo isn’t strictly required to get the status of a service…

Can’t help myself applying the principle of least privilege :slight_smile:


(Brian Orpin) #13

:smile:

Running DietPi, I tend to do everything as root, so I am never sure when you do and when you don’t!


(Brian Orpin) #14

Thought I’d follow up here rather than on GitHub.

I’ve never used feedwriter as my installs run on a VM, but I thought - why not!!

I used the install commands but ended up in a auto-restart loop -

● feedwriter.service - Emoncms feedwriter script
   Loaded: loaded (/var/www/emoncms/scripts/services/feedwriter/feedwriter.service; enabled; vendor preset: enabled)
   Active: activating (auto-restart) since Thu 2018-12-27 10:43:23 GMT; 39s ago
     Docs: https://github.com/emoncms/emoncms
  Process: 24896 ExecStart=/usr/bin/php /var/www/emoncms/scripts/feedwriter.php (code=exited, status=0/SUCCESS)
 Main PID: 24896 (code=exited, status=0/SUCCESS)

I suspect I need a setting in settings.php but I deliberately did not do that to test how this works :smile:

Note The docs state the service is run by the user=pi and non Rasbian users will need to change the user in the service file. This is not correct as the service file does not (now) have a user entry.

So initial issues:

  • There are no errors in the EmonCMS error log.
  • There are no errors in the SystemCtl status to help me.

On checking the log using journalctl I could see that the error messages were being created (not sure why the status command was not showing them).

Started Emoncms feedwriter script.
Error: setting must be true: feed_settings['redisbuffer']['enabled']
feedwriter.service: Service hold-off time over, scheduling restart.
Stopped Emoncms feedwriter script.

I checked the systemd docs and I think the restart command should be changed from always to on-failure.

On doing that I get the following on doing a status command which makes much more sense to the user.

● feedwriter.service - Emoncms feedwriter script
   Loaded: loaded (/var/www/emoncms/scripts/services/feedwriter/feedwriter.service; enabled; vendor preset: enabled)
   Active: inactive (dead) since Thu 2018-12-27 11:02:27 GMT; 583ms ago
     Docs: https://github.com/emoncms/emoncms
  Process: 25040 ExecStart=/usr/bin/php /var/www/emoncms/scripts/feedwriter.php (code=exited, status=0/SUCCESS)
 Main PID: 25040 (code=exited, status=0/SUCCESS)

Dec 27 11:02:27 DietPi-EmoncmsVM systemd[1]: feedwriter.service: Service hold-off time over, scheduling restart.
Dec 27 11:02:27 DietPi-EmoncmsVM systemd[1]: Stopped Emoncms feedwriter script.
Dec 27 11:02:27 DietPi-EmoncmsVM systemd[1]: Started Emoncms feedwriter script.
Dec 27 11:02:27 DietPi-EmoncmsVM feedwriter[25040]: Error: setting must be true: feed_settings['redisbuffer']['enabled']

Thoughts @TrystanLea

[edit] Should say though - it just works once the setting has been updated :grin:


(Glyn Hudson) #15

Thanks PR has been merged:


(Brian Orpin) #16

This question was never answered. I am updating an old test system which has the service-runner installed to use cron.

How do I remove that please so I can install the new version?


(Greebo) #17

The old system-runner was triggered from crontab (for the “pi” user on the EmonSD image), so editing the crontab to remove it is the simplest way to remove it before installing the systemd/redis based one. The emonsd update script used to reinstate the crontab entry though, so make sure that has also been updated first before you remove the crontab entry otherwise it will get put back next time you run the update script.

Once it is removed from crontab, kill the running process using sudo killall service-runner

I’m not familiar enough with the feedwriter change to provide details on “upgrading” it.


(Brian Orpin) #18

Thanks. I realise that, I just am not familiar with cron so I have no idea where to start to remove it and there are now no installation instructions I can back track as to how it was setup in the first place :frowning:

Really needs adding to the readmes.


(Greebo) #19

Ah… The easiest way to do that is crontab -e which “edits your crontab”. First you need to work out what user it is running as because you need to be that user to edit its crontab… If it is ‘pi’ for example, make sure you are logged on as ‘pi’, then crontab -e will fire up the defined editor for crontab (nano by default on a pi), either delete the line containing service-runner or prefix it with a # to comment it out, then save the “file”.

Use ps -ef | grep service-runner to work out what user it is running as.

You can check it using crontab -l (list your crontab)