Homebridge as Daemon

Posted on
Thu Sep 15, 2016 4:41 pm
gregss offline
Posts: 88
Joined: Sep 21, 2006
Location: Atlanta, GA

Homebridge as Daemon

In another form, several users were reporting having difficulty using Launchd to start Homebridge at startup. I've solved this problem and wanted to provide the steps that I took. Apologies if this has already been posted elsewhere — I couldn't find it.

The "problem" some users have had (including me), where Homebridge won't run successfully at start-up, but will run fine when started manually, is that the Indigo web server isn't yet fully alive when Homebridge is launched in this manner. This results in a blank set of devices, because they couldn't be loaded from Indigo. The solution is to make sure that the web server is running as part of the launch process, via a script.

The first step is to follow the great instructions in this sub-forum for installing and setting up Homebridge and Homebridge-Indigo. I made a couple of tweaks:
  • I wanted to run Homebridge using a different user account (as I do with Indigo)
  • Rather than starting at login, I wanted Homebridge to start when the computer boots

Here's what I did, step by step:
    1. Create local user homebridge, in group Indigo, with home directory
    Disable log in for user
    2. In homebridge user directory, create .homebridge (if you followed the instructions, just move this from your user directory)
    3. Place copy of conf.json in this directory
    4. In /Library/Logs, create Homebridge directory
    5. Set owner (chown) of the Homebridge directory to homebridge:indigo
    6. Place copy of launch.sh in /Users/homebridge (this is your start-up script; see below)
    7. Make launch.sh executable (sudo chmod ug+x launch.sh)
    8. Set owner (chown) for all files under homebridge to homebridge:indigo
    9. Copy com.homebridge.server.plist to /Library/LaunchDaemons (tweaks for user this will run under, logging location, and method of launch)
    10. Set owner (chown) this plist file to root:wheel
    11. sudo launchctl load /Library/LaunchDaemons/com.homebridge.server.plist

Here's my launch.sh file:
Code: Select all
#!/bin/sh
export PATH=/usr/local/bin:/usr/bin:/opt/local/bin:$PATH
result="$(curl --silent --head [b]http://localhost:8176/[/b] | head -n 1 | egrep -e 'HTTP/[0-9]\.[0-9] (2..|3..|401)')"
echo $result >/Library/Logs/Homebridge/homebridge.out 2>&1
case $result in
    '')
   exit 1
   ;;
    *)     
   exec homebridge -U /Users/homebridge/.homebridge >>/Library/Logs/Homebridge/homebridge.out 2>&1
esac


Replace http://localhost:8176/ with the URL and port your server runs on — mine is http://indigo.myservername.private:8000.

The launch script above is where the "magic" happens. Using the command-line utility CURL, it looks for a success response (2xx or 3xx) from the server; or, it looks for a request to authenticate (401) — your username and password aren't required, because it can tell the difference between an authentication challenge for a valid page and a not-found error. If any one of these success responses are received, it will launch Homebridge. If not, Launchd will wait and try again until the server is launched.

Here's my com.homebridge.server.plist file:
Code: Select all
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
   <key>UserName</key>
   <string>homebridge</string>
   <key>GroupName</key>
   <string>indigo</string>
   <key>RunAtLoad</key>
   <true/>
   <key>KeepAlive</key>
   <dict>
      <key>Crashed</key>
      <true/>
      <key>SuccessfulExit</key>
      <false/>
   </dict>
   <key>StandardErrorPath</key>
   <string>/Library/Logs/Homebridge/homebridge.err</string>
   <key>StandardOutPath</key>
   <string>/Library/Logs/Homebridge/homebridge.out</string>
    <key>Label</key>
    <string>com.homebridge.server</string>
    <key>ProgramArguments</key>
    <array>
      <string>/Users/homebridge/launch.sh</string>
       </array>
       <key>EnvironmentVariables</key>
       <dict>
      <key>PATH</key>
      <string>/usr/local/bin/:$PATH</string>
   </dict>
</dict>
</plist>


Replace the UserName and GroupName with the right values for your set-up.

I hope this helps someone out!

Posted on
Thu Sep 15, 2016 7:23 pm
webdeck offline
Posts: 436
Joined: May 07, 2005

Re: Homebridge as Daemon

Thanks for sharing this. I actually do something similar with my setup, but I figured it was too unique and complex to try and provide instructions for it. I'm glad you tackled that one!

-Mike

Posted on
Wed Oct 05, 2016 8:40 am
berkinet offline
User avatar
Posts: 3290
Joined: Nov 18, 2008
Location: Berkeley, CA, USA & Mougins, France

Re: Homebridge as Daemon

gregss wrote:
In another form, several users were reporting having difficulty using Launchd to start Homebridge at startup. I've solved this problem and wanted to provide the steps that I took. Apologies if this has already been posted elsewhere — I couldn't find it.
... ...
Code: Select all
... ...
result="$(curl --silent --head [b]http://localhost:8176/[/b] | head -n 1 | egrep -e 'HTTP/[0-9]\.[0-9] (2..|3..|401)')"
... ...
...

Thanks for the tip. One note, in the shell snippet noted above, text formatting codes were included because the text was inside of code tags. The line should read:
Code: Select all
result="$(curl --silent --head http://localhost:8176/ | head -n 1 | egrep -e 'HTTP/[0-9]\.[0-9] (2..|3..|401)')"

Posted on
Wed Oct 05, 2016 10:27 am
Different Computers offline
User avatar
Posts: 2533
Joined: Jan 02, 2016
Location: East Coast

Re: Homebridge as Daemon

I'm going to try this over the weekend, though I won't bother with the "as other user" part.

Thanks in advance!

SmartThings refugee, so happy to be on Indigo. Monterey on a base M1 Mini w/Harmony Hub, Hue, DomoPad, Dynamic URL, Device Extensions, HomeKitLink, Grafana, Plex, uniFAP, Fantastic Weather, Nanoleaf, LED Simple Effects, Bond Home, Camect.

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 10 guests

cron