Skip to content

Homebridge as service

christo-steyn edited this page Dec 8, 2017 · 2 revisions

Systemd Service for homebridge (http://github.com/nfarina/homebridge)

# Defaults / Configuration options for homebridge # The following settings tells homebridge where to find the config.json file and where to persist the data (i.e. pairing and others) HOMEBRIDGE_OPTS=-U /var/lib/homebridge

# If you uncomment the following line, homebridge will log more # You can display this via systemd's journalctl: journalctl -f -u homebridge # DEBUG=* Raw homebridge.service [Unit] Description=Node.js HomeKit Server After=syslog.target network-online.target

[Service] Type=simple User=homebridge EnvironmentFile=/etc/default/homebridge # Adapt this to your specific setup (could be /usr/bin/homebridge) # See comments below for more information ExecStart=/usr/local/bin/homebridge $HOMEBRIDGE_OPTS Restart=on-failure RestartSec=10 KillMode=process

[Install] WantedBy=multi-user.target

On newer Raspberry Pi and Debian systems (i.e. jessie based), managing of services with init.d/sysvinit is (transparently) replaced with systemd. If you wish to use systemd for running Homebridge on boot, you can follow these instructions. Familiarity with Linux concepts, user and rights management etc. are assumed.

As you can see, the service definition is much shorter than a comparable init.d script.

Download the two files and place homebridge under /etc/default and homebridge.service under /etc/systemd/system on your Raspberry Pi.

ATTENTION: Depending on how you installed nodejs (as package or tarball) and homebridge, you might have to change the line ExecStart in the file homebridge.service from /usr/local/bin/homebridge to /usr/bin/homebridge. The easiest way to find out which one to use is to issue the command which homebridge after you have installed homebridge on your system.

pi@pi:~ $ which homebridge /usr/local/bin/homebridge

# Configuration

In order to use the systemd service as is, the following folders and user have to exists:

A system user named homebridge. You can easily create this user with sudo useradd -M --system homebridge or choose a different name A directory called /var/lib/homebridge, writable by the user created above, and a corresponding config.json file in that directory. Homebridge by default looks for its configuration in /home//.homebridge. This is unsuitable for services and the -U /var/lib/homebridge flag ensures the config is read from a different place. Then Enable and run the service (first time) with the following commands:

systemctl daemon-reload systemctl enable homebridge systemctl start homebridge You can check the status of the service by calling

systemctl status homebridge On subsequent reboots, it should start automatically, if not, use the journalctl -u homebridge to check the error cause.

Notes

The service will restart after 10 seconds if it fails for any reason (or if you kill it for example with kill -s SIGSEGV )