Skip to content

Commit 3453399

Browse files
dagobertfabaff
authored andcommitted
Changed wording and init scripts (home-assistant#4455)
I changed the init scripts to be more reliable. hass executable supports the option --log-file. Using this is much safer and does not lead to problems with permission and overwriting old logfiles. Also I added creating the directory and ownership in the install function. To prevent log file groth I added a logrotate rule. I moved the update-rc.d command you had in your manual into the install function since you already ad it in the uninstall function. This prevents more copy&paste errors. I changed the PID file to be located in an extra directory. This way you do not have to to this hack with writing a fake PID file in the install function. Also, I read many users having problems that hass daemon is not starting (see eg. https://community.home-assistant.io/t/autostart-virtualenv-ubuntu-14-04/2120/37) without showing any errors. This is due to a missing PID file or its wrong permissions. By changing as stated and adding a test this problem should not arise any more. I changes some wording in the manual to make it more understandable.
1 parent 2115f18 commit 3453399

File tree

1 file changed

+124
-44
lines changed

1 file changed

+124
-44
lines changed

source/_docs/autostart/init.d.markdown

Lines changed: 124 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Home Assistant can run as a daemon within init.d with the script below.
1414

1515
### {% linkable_title 1. Copy script %}
1616

17-
Copy the script at the end of this page to `/etc/init.d/hass-daemon`.
17+
Copy either the deamon script or the Python environment scrip at the end of this page to `/etc/init.d/hass-daemon` depending on your installation.
1818

1919
After that, set the script to be executable:
2020

@@ -26,27 +26,41 @@ $ sudo chmod +x /etc/init.d/hass-daemon
2626

2727
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.
2828

29-
### {% linkable_title 3. Change hass executable if required. %}
29+
### {% linkable_title 3. Change hass executable and other variables if required. %}
3030

31-
Some installation environments may require a change in the Home Assistant executable `hass`. Update script to set `HASS_BIN` to the appropriate `hass` executable path.
31+
Some installation environments may require a change in the Home Assistant executable `hass`. Update script to set `HASS_BIN` to the appropriate `hass` executable path. Please also check the other variables for the appropriate value. In general the defaults should work
3232

33-
### {% linkable_title 4. Register the daemon with Linux %}
33+
### {% linkable_title 4. Install this service %}
3434

3535
```bash
36-
$ sudo update-rc.d hass-daemon defaults
36+
$ sudo service hass-daemon install
3737
```
3838

39-
### {% linkable_title 5. Install this service %}
39+
### {% linkable_title 5. Create logrotate rule %}
40+
41+
This logrotate script at `/etc/logrotate.d/homeassistant` will create an outage of a few seconds every week at night. If you do not want this add `--log-rotate-days 7` to the `FLAGS` variable in the init script.
42+
43+
```
44+
/var/log/homeassistant/home-assistant.log
45+
{
46+
rotate 7
47+
daily
48+
missingok
49+
notifempty
50+
delaycompress
51+
compress
52+
postrotate
53+
invoke-rc.d hass-daemon restart > /dev/null
54+
endscript
55+
}
4056
41-
```bash
42-
$ sudo service hass-daemon install
4357
```
4458

4559
### {% linkable_title 6. Restart Machine %}
4660

4761
That's it. Restart your machine and Home Assistant should start automatically.
4862

49-
If HA does not start, check the log file output for errors at `/var/opt/homeassistant/home-assistant.log`
63+
If HA does not start, check the log file output for errors at `/var/log/homeassistant/home-assistant.log`
5064

5165
### {% linkable_title Extra: Running commands before hass executes %}
5266

@@ -73,54 +87,87 @@ HASS_BIN="hass"
7387
RUN_AS="USER"
7488
PID_FILE="/var/run/hass.pid"
7589
CONFIG_DIR="/var/opt/homeassistant"
76-
FLAGS="-v --config $CONFIG_DIR --pid-file $PID_FILE --daemon"
77-
REDIRECT="> $CONFIG_DIR/home-assistant.log 2>&1"
90+
LOG_DIR="/var/log/homeassistant"
91+
LOG_FILE="$LOG_DIR/home-assistant.log"
92+
FLAGS="-v --config $CONFIG_DIR --pid-file $PID_FILE --log-file $LOG_FILE --daemon"
93+
7894

7995
start() {
96+
if [ ! -d "$PID_DIR" ]; then
97+
echo "It seems you did not run"
98+
echo -e "\tservice hass-daemon install"
99+
return 1
100+
fi
80101
if [ -f $PID_FILE ] && kill -0 $(cat $PID_FILE) 2> /dev/null; then
81102
echo 'Service already running' >&2
82103
return 1
83104
fi
84-
echo 'Starting service…' >&2
85-
local CMD="$PRE_EXEC $HASS_BIN $FLAGS $REDIRECT;"
86-
su -c "$CMD" $RUN_AS
87-
echo 'Service started' >&2
105+
echo -n 'Starting service… ' >&2
106+
local CMD="$PRE_EXEC $HASS_BIN $FLAGS;"
107+
su -s /bin/bash -c "$CMD" $RUN_AS
108+
if [ $? -ne 0 ]; then
109+
echo "Failed" >&2
110+
else
111+
echo 'Done' >&2
112+
fi
88113
}
89114

90115
stop() {
91-
if [ ! -f "$PID_FILE" ] || ! kill -0 $(cat "$PID_FILE") 2> /dev/null; then
116+
if [ ! -f "$PID_FILE" ] || ! kill -0 $(cat "$PID_FILE") 2> /dev/null; then
92117
echo 'Service not running' >&2
93118
return 1
94119
fi
95-
echo 'Stopping service…' >&2
120+
echo -n 'Stopping service… ' >&2
96121
kill $(cat "$PID_FILE")
97122
while ps -p $(cat "$PID_FILE") > /dev/null 2>&1; do sleep 1;done;
98-
echo 'Service stopped' >&2
123+
rm -f $PID_FILE
124+
echo 'Done' >&2
99125
}
100126

101127
install() {
102-
echo "Installing Home Assistant Daemon (hass-daemon)"
103-
echo "999999" > $PID_FILE
104-
chown $RUN_AS $PID_FILE
105-
mkdir -p $CONFIG_DIR
106-
chown $RUN_AS $CONFIG_DIR
128+
echo "Installing Home Assistant Daemon (hass-daemon)"
129+
update-rc.d hass-daemon defaults
130+
create_piddir
131+
mkdir -p $CONFIG_DIR
132+
chown $RUN_AS $CONFIG_DIR
133+
mkdir -p $LOG_DIR
134+
chown $RUN_AS $LOG_DIR
107135
}
108136

109137
uninstall() {
110-
echo -n "Are you really sure you want to uninstall this service? That cannot be undone. [yes|No] "
138+
echo "Are you really sure you want to uninstall this service? The INIT script will"
139+
echo -n "also be deleted! That cannot be undone. [yes|No] "
111140
local SURE
112141
read SURE
113142
if [ "$SURE" = "yes" ]; then
114143
stop
115-
rm -fv "$PID_FILE"
144+
remove_piddir
116145
echo "Notice: The config directory has not been removed"
117146
echo $CONFIG_DIR
147+
echo "Notice: The log directory has not been removed"
148+
echo $LOG_DIR
118149
update-rc.d -f hass-daemon remove
119150
rm -fv "$0"
120151
echo "Home Assistant Daemon has been removed. Home Assistant is still installed."
121152
fi
122153
}
123154

155+
create_piddir() {
156+
if [ ! -d "$PID_DIR" ]; then
157+
mkdir -p $PID_DIR
158+
chown $RUN_AS "$PID_DIR"
159+
fi
160+
}
161+
162+
remove_piddir() {
163+
if [ -d "$PID_DIR" ]; then
164+
if [ -e "$PID_FILE" ]; then
165+
rm -fv "$PID_FILE"
166+
fi
167+
rmdir -fv "$PID_DIR"
168+
fi
169+
}
170+
124171
case "$1" in
125172
start)
126173
start
@@ -158,60 +205,93 @@ esac
158205

159206
# /etc/init.d Service Script for Home Assistant
160207
# Created with: https://gist.github.com/naholyr/4275302#file-new-service-sh
161-
PRE_EXEC="cd /srv/homeassistant && python3 -m venv . && source bin/activate &&"
208+
PRE_EXEC="cd /srv/homeassistant; python3 -m venv .; source bin/activate;"
162209
# Typically /usr/bin/hass
163210
HASS_BIN="hass"
164-
RUN_AS="USER"
165-
PID_FILE="/var/run/hass.pid"
166-
CONFIG_DIR="/home/USER/.homeassistant"
167-
FLAGS="-v --config $CONFIG_DIR --pid-file $PID_FILE --daemon"
168-
REDIRECT="> $CONFIG_DIR/home-assistant.log 2>&1"
211+
RUN_AS="homeassistant"
212+
PID_DIR="/var/run/hass"
213+
PID_FILE="$PID_DIR/hass.pid"
214+
CONFIG_DIR="/home/$RUN_AS/.homeassistant"
215+
LOG_DIR="/var/log/homeassistant"
216+
LOG_FILE="$LOG_DIR/home-assistant.log"
217+
FLAGS="-v --config $CONFIG_DIR --pid-file $PID_FILE --log-file $LOG_FILE --daemon"
169218

170219
start() {
220+
if [ ! -d "$PID_DIR" ]; then
221+
echo "It seems you did not run"
222+
echo -e "\tservice hass-daemon install"
223+
return 1
224+
fi
171225
if [ -f $PID_FILE ] && kill -0 $(cat $PID_FILE) 2> /dev/null; then
172226
echo 'Service already running' >&2
173227
return 1
174228
fi
175-
echo 'Starting service…' >&2
176-
local CMD="$PRE_EXEC $HASS_BIN $FLAGS $REDIRECT;"
229+
echo -n 'Starting service… ' >&2
230+
local CMD="$PRE_EXEC $HASS_BIN $FLAGS;"
177231
su -s /bin/bash -c "$CMD" $RUN_AS
178-
echo 'Service started' >&2
232+
if [ $? -ne 0 ]; then
233+
echo "Failed" >&2
234+
else
235+
echo 'Done' >&2
236+
fi
179237
}
180238

181239
stop() {
182-
if [ ! -f "$PID_FILE" ] || ! kill -0 $(cat "$PID_FILE") 2> /dev/null; then
240+
if [ ! -f "$PID_FILE" ] || ! kill -0 $(cat "$PID_FILE") 2> /dev/null; then
183241
echo 'Service not running' >&2
184242
return 1
185243
fi
186-
echo 'Stopping service…' >&2
244+
echo -n 'Stopping service… ' >&2
187245
kill $(cat "$PID_FILE")
188246
while ps -p $(cat "$PID_FILE") > /dev/null 2>&1; do sleep 1;done;
189-
echo 'Service stopped' >&2
247+
rm -f $PID_FILE
248+
echo 'Done' >&2
190249
}
191250

192251
install() {
193-
echo "Installing Home Assistant Daemon (hass-daemon)"
194-
echo "999999" > $PID_FILE
195-
chown $RUN_AS $PID_FILE
196-
mkdir -p $CONFIG_DIR
197-
chown $RUN_AS $CONFIG_DIR
252+
echo "Installing Home Assistant Daemon (hass-daemon)"
253+
update-rc.d hass-daemon defaults
254+
create_piddir
255+
mkdir -p $CONFIG_DIR
256+
chown $RUN_AS $CONFIG_DIR
257+
mkdir -p $LOG_DIR
258+
chown $RUN_AS $LOG_DIR
198259
}
199260

200261
uninstall() {
201-
echo -n "Are you really sure you want to uninstall this service? That cannot be undone. [yes|No] "
262+
echo "Are you really sure you want to uninstall this service? The INIT script will"
263+
echo -n "also be deleted! That cannot be undone. [yes|No] "
202264
local SURE
203265
read SURE
204266
if [ "$SURE" = "yes" ]; then
205267
stop
206-
rm -fv "$PID_FILE"
268+
remove_piddir
207269
echo "Notice: The config directory has not been removed"
208270
echo $CONFIG_DIR
271+
echo "Notice: The log directory has not been removed"
272+
echo $LOG_DIR
209273
update-rc.d -f hass-daemon remove
210274
rm -fv "$0"
211275
echo "Home Assistant Daemon has been removed. Home Assistant is still installed."
212276
fi
213277
}
214278

279+
create_piddir() {
280+
if [ ! -d "$PID_DIR" ]; then
281+
mkdir -p $PID_DIR
282+
chown $RUN_AS "$PID_DIR"
283+
fi
284+
}
285+
286+
remove_piddir() {
287+
if [ -d "$PID_DIR" ]; then
288+
if [ -e "$PID_FILE" ]; then
289+
rm -fv "$PID_FILE"
290+
fi
291+
rmdir -fv "$PID_DIR"
292+
fi
293+
}
294+
215295
case "$1" in
216296
start)
217297
start

0 commit comments

Comments
 (0)