Skip to content

Instantly share code, notes, and snippets.

@nnance
Forked from johannrichard/homebridge
Last active November 19, 2018 16:11
Show Gist options
  • Save nnance/0411d2e349f9e2e8836a34b25d841dfb to your computer and use it in GitHub Desktop.
Save nnance/0411d2e349f9e2e8836a34b25d841dfb to your computer and use it in GitHub Desktop.
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=*
[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/bin/homebridge $HOMEBRIDGE_OPTS
Restart=on-failure
RestartSec=10
KillMode=process
[Install]
WantedBy=multi-user.target
@nnance
Copy link
Author

nnance commented Nov 19, 2018

This combines the settings found in this Gist under johannrichard/homebridge and this Guide into a single set of instructions.

On newer Raspberry Pi and Debian systems (Jessie +), managing of services with init.d is (transparently) replaced with systemd. If you wish to use systemd for running Homebridge on boot, you can follow these instructions. As you can see, the service definition is much shorter than a comparable init.d script.

To get started connect to the Rasberrypi with the following command:

ssh [email protected]

You will need to use the password that was setup during the installation process.

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/bin/homebridge to /usr/local/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

I installed node using the following commands:

curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
sudo apt-get install -y nodejs

As a result my ExecStart was setup for /usr/bin/homebridge and I have updated the homebridge.service file above accordingly.

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 useradd --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/<username>/.homebridge. This is unsuitable for services and the -U /var/lib/homebridge flag ensures the config is read from a different place.

Create user and the directory with the proper permissions:

sudo mkdir /var/lib/homebridge
sudo chown homebridge /var/lib/homebridge

This copies your current user’s config. This assumes you have already added accessories etc.

sudo cp ~/.homebridge/config.json /var/lib/homebridge
sudo cp -r ~/.homebridge/persist /var/lib/homebridge

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 <pid>)
  • You might also try the tips in this Tutorial.
  • To see the homebridge output, call sudo journalctl -au homebridge. Make sure to specify -n, or the QR code won't be displayed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment