Autostart Virtualenv Ubuntu 14.04

Put the absolute path to hass in the sudo command. Also, just to clarify, my other post only had the variables and the start function. The rest of the script was unchanged from the wiki link, and so I didn’t include it in the post.

Did you run service hass-daemon install? I never got any results until I performed that.

I put the absolute path into the sudo command and I get the same result. I can run it from command line but not luck from boot. Like you I only posted my variables and start function. The rest was left unchanged. I did do a service hass-daemon install both in the script above and after I changed to the absolute path. Not really sure what else to try.

Not sure either. Does mgranger own the /home/hass directory?

Yes mgranger is the owner of that directory.

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