|
| 1 | +--- |
| 2 | +layout: page |
| 3 | +title: "init.d daemon" |
| 4 | +description: "Documentation about setting up Home Assistant as a daemon within init.d." |
| 5 | +release_date: 2016-12-02 15:00:00 -0700 |
| 6 | +sidebar: true |
| 7 | +comments: false |
| 8 | +sharing: true |
| 9 | +footer: true |
| 10 | +--- |
| 11 | + |
| 12 | +Home Assistant can run as a daemon within init.d with the script below. |
| 13 | + |
| 14 | +### {% linkable_title 1. Copy script %} |
| 15 | + |
| 16 | +Copy the script at the end of this page to `/etc/init.d/hass-daemon`. |
| 17 | + |
| 18 | +After that, set the script to be executable: |
| 19 | + |
| 20 | +``` |
| 21 | +sudo chmod +x /etc/init.d/hass-daemon |
| 22 | +``` |
| 23 | + |
| 24 | +### {% linkable_title 2. Select a user. %} |
| 25 | + |
| 26 | +Create or pick a user that the Home Assistant daemon will run under. Update script to set `RUN_AS` to the username that should be used to execute hass. |
| 27 | + |
| 28 | +### {% linkable_title 3. Register the daemon with Linux %} |
| 29 | + |
| 30 | +``` |
| 31 | +sudo update-rc.d hass-daemon defaults |
| 32 | +``` |
| 33 | + |
| 34 | +### {% linkable_title 4. Install this service %} |
| 35 | + |
| 36 | +``` |
| 37 | +sudo service hass-daemon install |
| 38 | +``` |
| 39 | + |
| 40 | +### {% linkable_title 5. Restart Machine %} |
| 41 | + |
| 42 | +That's it. Restart your machine and Home Assistant should start automatically. |
| 43 | + |
| 44 | +If HA does not start, check the log file output for errors at `/var/opt/homeassistant/home-assistant.log` |
| 45 | + |
| 46 | +### {% linkable_title Extra: Running commands before hass executes %} |
| 47 | + |
| 48 | +If any commands need to run before executing hass (like loading a virutal environment), put them in PRE_EXEC. This command must end with a semicolon. |
| 49 | + |
| 50 | +### {% linkable_title Daemon script %} |
| 51 | + |
| 52 | +``` |
| 53 | +#!/bin/sh |
| 54 | +### BEGIN INIT INFO |
| 55 | +# Provides: hass |
| 56 | +# Required-Start: $local_fs $network $named $time $syslog |
| 57 | +# Required-Stop: $local_fs $network $named $time $syslog |
| 58 | +# Default-Start: 2 3 4 5 |
| 59 | +# Default-Stop: 0 1 6 |
| 60 | +# Description: Home\ Assistant |
| 61 | +### END INIT INFO |
| 62 | +
|
| 63 | +# /etc/init.d Service Script for Home Assistant |
| 64 | +# Created with: https://gist.github.com/naholyr/4275302#file-new-service-sh |
| 65 | +PRE_EXEC="" |
| 66 | +RUN_AS="USER" |
| 67 | +PID_FILE="/var/run/hass.pid" |
| 68 | +CONFIG_DIR="/var/opt/homeassistant" |
| 69 | +FLAGS="-v --config $CONFIG_DIR --pid-file $PID_FILE --daemon" |
| 70 | +REDIRECT="> $CONFIG_DIR/home-assistant.log 2>&1" |
| 71 | +
|
| 72 | +start() { |
| 73 | + if [ -f $PID_FILE ] && kill -0 $(cat $PID_FILE) 2> /dev/null; then |
| 74 | + echo 'Service already running' >&2 |
| 75 | + return 1 |
| 76 | + fi |
| 77 | + echo 'Starting service…' >&2 |
| 78 | + local CMD="$PRE_EXEC hass $FLAGS $REDIRECT;" |
| 79 | + su -c "$CMD" $RUN_AS |
| 80 | + echo 'Service started' >&2 |
| 81 | +} |
| 82 | +
|
| 83 | +stop() { |
| 84 | + if [ ! -f "$PID_FILE" ] || ! kill -0 $(cat "$PID_FILE") 2> /dev/null; then |
| 85 | + echo 'Service not running' >&2 |
| 86 | + return 1 |
| 87 | + fi |
| 88 | + echo 'Stopping service…' >&2 |
| 89 | + kill $(cat "$PID_FILE") |
| 90 | + while ps -p $(cat "$PID_FILE") > /dev/null 2>&1; do sleep 1;done; |
| 91 | + echo 'Service stopped' >&2 |
| 92 | +} |
| 93 | +
|
| 94 | +install() { |
| 95 | + echo "Installing Home Assistant Daemon (hass-daemon)" |
| 96 | + echo "999999" > $PID_FILE |
| 97 | + chown $RUN_AS $PID_FILE |
| 98 | + mkdir -p $CONFIG_DIR |
| 99 | + chown $RUN_AS $CONFIG_DIR |
| 100 | +} |
| 101 | +
|
| 102 | +uninstall() { |
| 103 | + echo -n "Are you really sure you want to uninstall this service? That cannot be undone. [yes|No] " |
| 104 | + local SURE |
| 105 | + read SURE |
| 106 | + if [ "$SURE" = "yes" ]; then |
| 107 | + stop |
| 108 | + rm -fv "$PID_FILE" |
| 109 | + echo "Notice: The config directory has not been removed" |
| 110 | + echo $CONFIG_DIR |
| 111 | + update-rc.d -f hass-daemon remove |
| 112 | + rm -fv "$0" |
| 113 | + echo "Home Assistant Daemon has been removed. Home Assistant is still installed." |
| 114 | + fi |
| 115 | +} |
| 116 | +
|
| 117 | +case "$1" in |
| 118 | + start) |
| 119 | + start |
| 120 | + ;; |
| 121 | + stop) |
| 122 | + stop |
| 123 | + ;; |
| 124 | + install) |
| 125 | + install |
| 126 | + ;; |
| 127 | + uninstall) |
| 128 | + uninstall |
| 129 | + ;; |
| 130 | + restart) |
| 131 | + stop |
| 132 | + start |
| 133 | + ;; |
| 134 | + *) |
| 135 | + echo "Usage: $0 {start|stop|restart|install|uninstall}" |
| 136 | +esac |
| 137 | +``` |
0 commit comments