I have managed this issue. It looks that hass does not create the file that after restart of the system autoremoved from /var/run folder. So I add to the start script the creating of the file from install function.
Now it is working!
Full autorun script:
#!/bin/sh
### BEGIN INIT INFO ###########################################
# Provides: hass
# Required-Start: $local_fs $network $named $time $syslog
# Required-Stop: $local_fs $network $named $time $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Description: Home\ Assistant
### END INIT INFO #############################################
# /etc/init.d Service Script for Home Assistant
# Created with: https://gist.github.com/naholyr/4275302#file-new-service-sh
#
# Installation:
# 1) If any commands need to run before executing hass (like loading a
# virtual environment), put them in PRE_EXEC. This command must end with a semicolon.
# 2) Set RUN_AS to the username that should be used to execute hass.
# 3) Copy this script to /etc/init.d/
# sudo cp hass-daemon /etc/init.d/hass-daemon
# sudo chmod +x /etc/init.d/hass-daemon
# 4) Register the daemon with Linux
# sudo update-rc.d hass-daemon defaults
# 5) Install this service
# sudo service hass-daemon install
# 6) Restart Machine
#
# After installation, HA should start automatically. If HA does not start,
# check the log file output for errors.
# /var/opt/homeassistant/home-assistant.log
PRE_EXEC=". /srv/hass/bin/activate;"
RUN_AS="hass"
PID_FILE="/var/run/hass.pid"
CONFIG_DIR="/home/hass/.homeassistant"
FLAGS="-v --config $CONFIG_DIR --pid-file $PID_FILE --daemon"
REDIRECT="> $CONFIG_DIR/home-assistant.log 2>&1"
start() {
if [ -f $PID_FILE ] && kill -0 $(cat $PID_FILE) 2> /dev/null; then
echo 'Service already running' >&2
return 1
fi
echo 'Starting service…' >&2
local CMD="$PRE_EXEC /srv/hass/bin/hass $FLAGS $REDIRECT;"
#su -c "$CMD" $RUN_AS
echo "999999" > $PID_FILE
chown $RUN_AS $PID_FILE
sudo -u hass -H bash -c "$PRE_EXEC /srv/hass/bin/hass $FLAGS | tee -a $CONFIG_DIR/home-assistant.log"
echo 'Service started' >&2
}
stop() {
if [ ! -f "$PID_FILE" ] || ! kill -0 $(cat "$PID_FILE") 2> /dev/null; then
echo 'Service not running' >&2
return 1
fi
echo 'Stopping service…' >&2
kill $(cat "$PID_FILE")
while ps -p $(cat "$PID_FILE") > /dev/null 2>&1; do sleep 1;done;
echo 'Service stopped' >&2
}
install() {
echo "Installing Home Assistant Daemon (hass-daemon)"
echo "999999" > $PID_FILE
chown $RUN_AS $PID_FILE
mkdir -p $CONFIG_DIR
chown $RUN_AS $CONFIG_DIR
}
uninstall() {
echo -n "Are you really sure you want to uninstall this service? That cannot be undone. [yes|No] "
local SURE
read SURE
if [ "$SURE" = "yes" ]; then
stop
rm -fv "$PID_FILE"
echo "Notice: The config directory has not been removed"
echo $CONFIG_DIR
update-rc.d -f hass-daemon remove
rm -fv "$0"
echo "Home Assistant Daemon has been removed. Home Assistant is still installed."
fi
}
case "$1" in
start)
start
;;
stop)
stop
;;
install)
install
;;
uninstall)
uninstall
;;
restart)
stop
start
;;
*)
echo "Usage: $0 {start|stop|restart|install|uninstall}"
esac
In the manual installation using a virtual enviroment, i created the user homeassistant in my case. Then, my hass file that execute HA is in: /srv/homeassistant/homeassistant_venv/bin/, with other files like “activate”.
My config yaml file is in this route: /home/homeassistant/.homeassistant/.
This is the modified part of the script for the /etc/init.d/hass-daemon file:
I’m also having problems getting hass to start in a virtualenv with upstart. There seems to be a break in the documentation or code which is blocking the client from starting.
If anyone has this working please share your configuration.
Hi.
It was trouble for me to.
My user name is hass
So you need to do manipulation with autostart rc.init scripts. Just use command to activate virtual inv:
PRE_EXEC=“. /srv/hass/bin/activate;”
My “hass-daemon” autostart script is down here. Lots of the comments was there before.
# /etc/init.d Service Script for Home Assistant
# Created with: https://gist.github.com/naholyr/4275302#file-new-service-sh
#
# Installation:
# 1) If any commands need to run before executing hass (like loading a
# virtual environment), put them in PRE_EXEC. This command must end with a semicolon.
# 2) Set RUN_AS to the username that should be used to execute hass.
# 3) Copy this script to /etc/init.d/
# sudo cp hass-daemon /etc/init.d/hass-daemon
# sudo chmod +x /etc/init.d/hass-daemon
# 4) Register the daemon with Linux
# sudo update-rc.d hass-daemon defaults
# 5) Install this service
# sudo service hass-daemon install
# 6) Restart Machine
#
# After installation, HA should start automatically. If HA does not start,
# check the log file output for errors.
# /var/opt/homeassistant/home-assistant.log
PRE_EXEC=". /srv/hass/bin/activate;"
RUN_AS="hass"
PID_FILE="/var/run/hass.pid"
CONFIG_DIR="/home/hass/.homeassistant"
FLAGS="-v --config $CONFIG_DIR --pid-file $PID_FILE --daemon"
REDIRECT="> $CONFIG_DIR/home-assistant.log 2>&1"
uninstall() {
echo -n "Are you really sure you want to uninstall this service? That cannot be undone. [yes|No] "
local SURE
read SURE
if [ "$SURE" = "yes" ]; then
stop
rm -fv "$PID_FILE"
echo "Notice: The config directory has not been removed"
echo $CONFIG_DIR
update-rc.d -f hass-daemon remove
rm -fv "$0"
echo "Home Assistant Daemon has been removed. Home Assistant is still installed."
fi
}
Thanks for sharing Actpohomoc, it helped me realize that I missed another raw call to ‘hass’ in the startup script. I think I’ll create a new variable called BINARY_EXECUTABLE and set it to the location of the virtualenv bin/hass file: BINARY_EXECUTABLE=/srv/hass/bin/hass so that makes my local CMD line look a little cleaner and pushes the necessary changes up top to the variables section: local CMD="$PRE_EXEC $BINARY_EXECUTABLE $FLAGS $REDIRECT;"
Are you aware that you’re setting variables and never using them? Your sudo line ignores both the RUN_AS and the local CMD variables.
And are you sure that we need to manually create the PID file? That step looks like it was pulled from the install() section.
Another weakness of the original script is that it doesn’t verify that the service is running before declaring “Service started”. Are you sure the service is actually starting?
What can you advise to change this?
I think to move to 16 version and I know that there are another system in startups.
Can you publish all your startup script? Thank you.
I can’t share my script because it doesn’t work. I suppose if you are able to load the webpage after a system reboot then you know that the script is running. Wish I had that much success!
# /etc/init.d Service Script for Home Assistant
# Created with: Sample /etc/init.d script · GitHub
#
# Installation:
# 1) If any commands need to run before executing hass (like loading a
# virtual environment), put them in PRE_EXEC. This command must end with a semicolon.
# 2) Set RUN_AS to the username that should be used to execute hass.
# 3) Copy this script to /etc/init.d/
# sudo cp hass-daemon /etc/init.d/hass-daemon
# sudo chmod +x /etc/init.d/hass-daemon
# 4) Register the daemon with Linux
# sudo update-rc.d hass-daemon defaults
# 5) Install this service
# sudo service hass-daemon install
# 6) Restart Machine
#
# After installation, HA should start automatically. If HA does not start,
# check the log file output for errors.
# /var/opt/homeassistant/home-assistant.log
uninstall() {
echo -n "Are you really sure you want to uninstall this service? That cannot be undone. [yes|No] "
local SURE
read SURE
if [ “$SURE” = “yes” ]; then
stop
rm -fv “$PID_FILE”
echo “Notice: The config directory has not been removed”
echo $CONFIG_DIR
update-rc.d -f hass-daemon remove
rm -fv “$0”
echo “Home Assistant Daemon has been removed. Home Assistant is still installed.”
fi
}
case “$1” in
start)
start
;;
stop)
stop
;;
install)
install
;;
uninstall)
uninstall
;;
restart)
stop
start
;; status) echo “Checking status of $HASSD” status_of_proc -p “$PIDFILE” “$HASSD” “$NAME” || exit $? ;;
*)
echo “Usage: $0 {start|stop|restart|install|uninstall|status}”
esac
Nice mods Actpohomoc! Thanks for sharing this. I’m not too familiar with github but have you tried to push the changes up or request a pull/merge or whatever it’s called?
[quote=“Actpohomoc, post:32, topic:2120”]
Hi! I’ve changed the script …[/quote]
I got around to trying this and got missing LSB information errors during the script installation until I added another line above the description line: # Short-Description: Home\ Assistant
Other than that this was a clean paste-in for me! Thanks Actpohomoc!
After adding this script, the homeassistant gui is inaccessible after about a days uptime. The vm I’m using (32bit ubuntu) is still up, and the “service hass-daemon status” shows as running. I’m unable to restart the service, but I can restart the VM from VirtualBox and hass will work again for about a day.
Can you point me in the right direction for troubleshooting this?
Hello,
Maybe you already resolve this, in my case using a Raspberry the quick solution was to include the lines inside the “install” option in the “start” option, so they are executed always before starting the daemon.
Those lines only create the pid file and give the user rights to it.
With the PID file create the daemon starts properly.