Autostart Virtualenv Ubuntu 14.04

I have exactly the same. After reboot does not work. Than I run “service hass-daemon install” again. After it “service hass-daemon start” works.

How I can make it autostars w/o my manual typing work? :slight_smile:

16-09-17 23:25:30 homeassistant.core: Bus:Handling <Event state_changed[L]: entity_id=sun.sun, old_state=<state sun.sun=below_horizon; friendly_name=Sun, azimuth=0, nex
16-09-17 23:25:30 homeassistant.core: Stopping
16-09-17 23:25:30 homeassistant.core: Bus:Handling <Event homeassistant_stop[L]>

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

Hi to everyone,

Yesterday I installed HA in my raspberry using the “manual installation”, and i wanted to make the Autostart. I tried few things and didn’t worked.

But now, after make few changes in the script, worked using stating the service and stopping. Now i just need to try if after reboot works, but i think that will work if daemon-service worked. I used this script that someone before added: https://raw.githubusercontent.com/home-assistant/home-assistant/dev/script/hass-daemon.

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:

PRE_EXEC=“source /srv/homeassistant/homeassistant_venv/bin/activate;”
RUN_AS=“homeassistant”
PID_FILE="/var/run/hass.pid"
CONFIG_DIR="/home/homeassistant/.homeassistant"
FLAGS="-v --config $CONFIG_DIR --pid-file $PID_FILE --daemon"
REDIRECT="> $CONFIG_DIR/home-assistant.log 2>&1"

Hope i can help to someone.

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.

#!/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

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? :slight_smile:

You are right. My system doesn’t know about service are running.


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!

I did however get it launching automatically using cron:
# start home-assistant @reboot sleep 100; sudo -u hass -H source /srv/hass/bin/activate >/dev/null 2>&1 @reboot sleep 105; sudo -u hass -H /srv/hass/bin/hass >/dev/null 2>&1

However this is not running in the virtual environment. Hanging this up for now…

It won’t work like that. Make a two-line script:

#!/bin/bash
source /srv/hass/bin/activate
/srv/hass/bin/hass

chmod +x it
Then your cron entry will be
@reboot sleep 100; sudo -u hass -H /path/to/script >/dev/null 2>&1

Am I correct that PRE_EXEC=“. /srv/hass/bin/activate;” can be replaced by “source /srv/hass/bin/activate;”?

. means the same as source to most shells, yes

Hi! I’ve changed the script by comparing with other startup scripts and found solution to get current status of the process.

#!/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: 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

PRE_EXEC=“. /srv/hass/bin/activate;”
RUN_AS=“hass”
PID_FILE=“/var/run/hass.pid”
CONFIG_DIR=“/home/hass/.homeassistant”
HASSD=“/srv/hass/bin/hass”
NAME=“/srv/hass/bin/python3 /srv/hass/bin/hass”

FLAGS=“-v --config $CONFIG_DIR --pid-file $PID_FILE --daemon”
REDIRECT=“> $CONFIG_DIR/home-assistant.log 2>&1”

# Using LSB functions to perform the operations
. /lib/lsb/init-functions

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 $HASSD $FLAGS $REDIRECT;”
#su -c “$CMD” $RUN_AS
echo “999999” > $PID_FILE
chown $RUN_AS $PID_FILE
sudo -u hass -H bash -c “$PRE_EXEC $HASSD $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
;;
status)
echo “Checking status of $HASSD”
status_of_proc -p “$PIDFILE” “$HASSD” “$NAME” || exit $?
;;
*)
echo “Usage: $0 {start|stop|restart|install|uninstall|status}”
esac

4 Likes

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? :slight_smile:

I have not finished the course about it yet. :frowning: I’ll try to do it later. Thank you.

[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!

1 Like

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?

been reading this today.

hope this script helps u all. works for me :slight_smile:

its modified version of script https://github.com/home-assistant/home-assistant/pull/214/files

just edit variables to your system configuration

PRE_EXEC=“source /srv/homeassistant/homeassistant_venv/bin/activate;”
CONFIG_DIR=/home/homeassistant/.homeassistant
SCRIPT="/srv/homeassistant/homeassistant_venv/bin/hass -c $CONFIG_DIR"
RUNAS=homeassistant
RUNDIR=/srv/homeassistant/homeassistant_venv/bin
PIDFILE=/var/run/hass.pid
LOGFILE=/home/homeassistant/.homeassistant/homeassistant.log

#!/bin/sh
### BEGIN INIT INFO
# Provides:          homeassistant
# 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) Populate RUNAS and RUNDIR folders
#  2) Create Log  --  sudo touch /var/log/homeassistant.log
#  3) Set Log Ownership  --  sudo chown USER:GROUP /var/log/homeassistant.log
#  4) Create PID File  --  sudo touch /var/run/homeassistant.pid
#  5) Set PID File Ownership  --  sudo chown USER:GROUP /var/run/homeassistant.pid
#  6) Install init.d script  --  cp homeassistant.daemon /etc/init.d/homeassistant
#  7) Setup HA for Auto Start  --  sudo update-rc.d homeassistant defaults
#  8) Run HA  --  sudo service homeassistant start
#
# After installation, HA should start automatically. If HA does not start,
# check the log file output for errors. (/var/log/homeassistant.log)
#
# For this script, it is assumed that you are using a local Virtual Environment
# per the install directions as http://home-assistant.io
# If you are not, the SCRIPT variable must be modified to point to the correct
# Python environment.

PRE_EXEC="source /srv/homeassistant/homeassistant_venv/bin/activate;"
CONFIG_DIR=/home/homeassistant/.homeassistant
SCRIPT="/srv/homeassistant/homeassistant_venv/bin/hass -c $CONFIG_DIR"
RUNAS=homeassistant
RUNDIR=/srv/homeassistant/homeassistant_venv/bin
PIDFILE=/var/run/hass.pid
LOGFILE=/home/homeassistant/.homeassistant/homeassistant.log

start() {
  if [ -f /var/run/$PIDNAME ] && kill -0 $(cat /var/run/$PIDNAME); then
    echo 'Service already running' >&2
    return 1
  fi
  echo 'Starting service…' >&2
  local CMD="cd $RUNDIR; $PRE_EXEC $SCRIPT &> \"$LOGFILE\" & echo \$!"
  su -c "$CMD" $RUNAS > "$PIDFILE"
  echo 'Service started' >&2
}

stop() {
  if [ ! -f "$PIDFILE" ] || ! kill -0 $(cat "$PIDFILE"); then
    echo 'Service not running' >&2
    return 1
  fi
  echo 'Stopping service…' >&2
  kill -15 $(cat "$PIDFILE") && rm -f "$PIDFILE"
  echo 'Service stopped' >&2
}

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 -f "$PIDFILE"
    echo "Notice: log file is not be removed: '$LOGFILE'" >&2
    update-rc.d -f homeassistant remove
    rm -fv "$0"
  fi
}

case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  uninstall)
    uninstall
    ;;
  retart)
    stop
    start
    ;;
  *)
    echo "Usage: $0 {start|stop|restart|uninstall}"
esac
2 Likes

the last part helped me,

the default manual installation is now /srv/homeassistant/bin/activate; in stead of /srv/homeassistant/homeassistant_venv/bin/activate;

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.

1 Like

I was having the same problem in Raspbian, my solution was to do this:

local CMD="$PRE_EXEC; $HASS_BIN; $FLAGS; $REDIRECT;"

inserting the semicolons after each set of commands did the trick.

Hope it helps.