Monday, 16 January 2017

Homebridge, autostart with systemd


Updated 2017-10-08 !

systemd is service manger which autostart jobs and it will even

               restart the homebridge job if it fails  



!!! Disable any other homebridge autostart job before You enable systemd  !!! 

If You followed my earlier setup the homebridge command is placed in

/usr/bin/homebridge

If You used another setup check with 

$ which homebridge

so the path in file ”homebridge.service” will be correct

ExecStart=/usr/bin/homebridge

Create the service file for systemd


$ sudo nano /etc/systemd/system/homebridge.service

and add the following configuration

[Unit]
Description=Node.js HomeKit Server 
After=syslog.target network-online.target
         
[Service]
Type=simple
User=pi
# Could be /usr/local/bin/homebridge
ExecStart=/usr/bin/homebridge
Restart=on-failure
RestartSec=10
KillMode=process
         
[Install]
WantedBy=multi-user.target

Then the following commands to get it up and running

$ sudo systemctl daemon-reload
$ sudo systemctl enable homebridge
Created symlink from /etc/systemd/system/multiuser.target.wants/homebridge.service
 to /etc/systemd/system/homebridge.service.
$ sudo systemctl start homebridge

With the following command 

$ sudo systemctl status homebridge -l

You will get something like this if You succeed 

$ sudo systemctl status homebridge
homebridge.service - Node.js HomeKit Server
   Loaded: loaded (/etc/systemd/system/homebridge.service; enabled)
   Active: active (running) since Mon 2017-01-16 18:37:23 UTC; 2min 49s ago
 Main PID: 3047 (homebridge)
   CGroup: /system.slice/homebridge.service
           └─3047 homebridge
                            


This setup is picked up from 

https://gist.github.com/johannrichard/0ad0de1feb6adb9eb61a

and from homebridge 

https://github.com/nfarina/homebridge/wiki/Running-HomeBridge-on-a-Raspberry-Pi

but changed a little to do it as simple as possible.
I know that this solution is not best practice and if the setup fails it is hard to debug. But sometimes simplicity wins, or ? 

If You have problems and would like to clean up, use the following commands 

$ sudo systemctl stop homebridge
$ sudo systemctl disable homebridge

and then delete the ”homebridge.service” file


Interested in the ultimate Weather WEB-site

7 comments:

  1. Hur vet man att homebridge hittar config filen enligt exemplet ovan?

    ReplyDelete
  2. Since the systemd service manager is executing as "pi" and Homebridge(HB) is installed with "pi" it will look for the "config.json" in the default location. The same as if You execute the HB command on a command line.

    ReplyDelete
  3. Thanks for quick replies. Do you mean default location for systemd or homebridge?

    ReplyDelete
  4. Is systemd your preferred way to run homebridge now?

    ReplyDelete
  5. Default location for homebridge "config.json". I think that systemd allways is the preferred way to start any background job, since it take cares of any failure and restarts the job. But it could be easier to set up......

    ReplyDelete
  6. Tack för hjälpen med detta, fungerar utmärkt!
    Kan rekommendera Homekit-pluginet för Node-Red, värt att testa på!

    ReplyDelete