July 10, 2020

Homebridge, autostart with systemd

Updated 2020-07-10 !

systemd is service manger which autostart jobs and it will even 
restart the Homebridge(HB) job if it fails !  

Keep it simple !! New post with a script which handles the setup automagically !! or use the latest HB install where this is included.

Other HomeKit posts on this Blogg

!!! 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                                               

If You want to stop homebridge temporarily and for example run it from the command line just stop the service with 

$ sudo systemctl stop homebridge

and start with 

$ homebridge

after the testing, exit and start again with 

$ sudo systemctl start homebridge

This setup is picked up from Johan and from homebridge but optimized with retained function.
Temperatur measure with DS18B20

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.

A post for connecting temperature sensors direct to RPi and show in Homekit.

8 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
  7. Thank you very much, this worked perfectly

    ReplyDelete

Feel free to leave a comment ! ... but due to a lot of spam comments I have to moderate them. Will reply ASAP !