Autostart Virtualenv Ubuntu 14.04

I have setup HA in a Virtualenv on an Ubuntu 14.04 server. I am trying to modify the example init.d script so the daemon will start on boot but I cannot get HA to start. I am able to start HA manually within the Virtualenv by switching to the hass user and activating the Virtualenv.

Example script I used: https://raw.githubusercontent.com/home-assistant/home-assistant/dev/script/hass-daemon

My config section in the init.d script is as follows (I have not modified any other part of the file:

PRE_EXEC=""
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"

What am I missing?

Thank you.

PRE_EXEC="source /home/hass/home-assistant/bin/activate;"

or wherever your venv activate script is

I have same problem. Lubuntu 14.04 with upstart.
I installed HASS in an virtual environment, followed the guide exactly.
Downloaded the script: https://raw.githubusercontent.com/home-assistant/home-assistant/dev/script/hass-daemon

Did this:

# Installation:
#   1) 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.
#   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

My config:

PRE_EXEC="source /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"

it doesnā€™t start, no log in /var/opt/homeassistant/home-assistant.log

What am i doing wrong?

With that config, your log file is at /home/hass/.homeassistant/home-assistant.log . What does it say?

Is your virtual env in /srv/hass/bin/activate ?

Found the log. what i can tell it doesnā€™t show anyting. Old stuff. Can i delete it and the restart my computer?
Followed this.


I gues my virtual environment should be in /srv/hass/bin/activate? i see the file, activate there.

Tested this. Shouldnā€™t it say sopped instead of service not running?

kitchen@kitchen:/etc/init.d$ sudo service hass-daemon install
Installing Home Assistant Daemon (hass-daemon)
kitchen@kitchen:/etc/init.d$ sudo service hass-daemon start
Starting serviceā€¦
Service started
kitchen@kitchen:/etc/init.d$ sudo service hass-daemon stop
Service not running
kitchen@kitchen:/etc/init.d$

Is this OK? the pid-file in /var/run

-rw-r--r--  1 hass       nogroup       7 Jul 11 23:18 hass.pid

Deleted my home-assistant.log
Rebooted. No new log created.

How does it know what to run? Nothing pointing at /srv/hass/bin/hass

Hi again guys.
Seems to be hard to get support on this one.
Who wrote the script?

Anyway, i tok an easy way.
A added

sudo -u hass -H /srv/hass/bin/hass

In my rc.local
It works, now i have hass running at boot.
Is this ā€œwrongā€ way to do it?
is there a disadvantage if i do it this way?

Seems to be hard to get support on this one.

Well, you didnā€™t really answer my questions.

How does it know what to run? Nothing pointing at /srv/hass/bin/hass

It looks in the PATH. /srv/hass/bin may not be on it.

Service started

This is probably a lie.

Is there a disadvantage if i do it this way?

No easy way to stop/start without rebooting.

What happens when you manually run:

PRE_EXEC="source /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" CMD="$PRE_EXEC hass $FLAGS $REDIRECT;" su -c "$CMD" $RUN_AS

Oh, i thought i did answer your questions.
I am glad you still are trying to help me.
Can i run the commands one by one or shall i type it in alla at once.
I tried to paste it in putty but it looked very strange.

I did install it, sudo service hass-daemon uninstall
Can i try typing it in without install the script?
Or shall i reinstall it first?

BTW:
I found this on the internet.
ā€œSo ive worked it out, for some reason upstart doesnt like using ā€˜sourceā€™ so i changed the line from:ā€

source bin/activate

to

. bin/activate

ā€œand that works, dont know why though, so would be interested if someone could explain thisā€

An answer:

source is a bash built-in command but only a posix ā€œspecialā€ command.

Upstart runs sh -e when executing the script sections.

sh shell doesnā€™t understand source, only .


Found this in a forum.
I tried it but no luck.

Please copy and paste the commands in my last post in, as root, and then copy and paste the output you get here.

Okey.
I tried it. I hope i did it the right way.
First i tried it as root.

kitchen@kitchen:~$ sudo su
root@kitchen:/home/kitchen# PRE_EXEC="source /srv/hass/bin/activate;"
root@kitchen:/home/kitchen# RUN_AS="hass"
root@kitchen:/home/kitchen# PID_FILE="/var/run/hass.pid"
root@kitchen:/home/kitchen# CONFIG_DIR="/home/hass/.homeassistant"
root@kitchen:/home/kitchen# FLAGS="-v --config $CONFIG_DIR --pid-file $PID_FILE --daemon"
root@kitchen:/home/kitchen# REDIRECT="> $CONFIG_DIR/home-assistant.log 2>&1"
root@kitchen:/home/kitchen# CMD="$PRE_EXEC hass $FLAGS $REDIRECT;"
root@kitchen:/home/kitchen# su -c "$CMD" $RUN_AS

Nothing happends. No output at all.
Like i said earlier, i uninstalled the script. Is that an issue?

Then i tried it as the user, kitchen

kitchen@kitchen:~$ PRE_EXEC="source /srv/hass/bin/activate;"
kitchen@kitchen:~$ RUN_AS="hass"
kitchen@kitchen:~$ PID_FILE="/var/run/hass.pid"
kitchen@kitchen:~$ CONFIG_DIR="/home/hass/.homeassistant"
kitchen@kitchen:~$ FLAGS="-v --config $CONFIG_DIR --pid-file $PID_FILE --daemon"                                                                                                                                                             kitchen@kitchen:~$ REDIRECT="> $CONFIG_DIR/home-assistant.log 2>&1"
kitchen@kitchen:~$ CMD="$PRE_EXEC hass $FLAGS $REDIRECT;"
kitchen@kitchen:~$ su -c "$CMD" $RUN_AS
Password:

In the end it asked for password.
I have never set any password for the user, hass

I registered just to reply to this thread. I had been struggling with the startup script as well, and finally changed the su to sudo insteadā€¦

sudo -u hass -H /srv/hass/bin/hass $FLAGS

Anyway, hope that helps some one.

Hi.
Thank you for your effort.
Should i change

su -c "$CMD" $RUN_AS

to

sudo -c "$CMD" $RUN_AS

and where shall put sudo -u hass -H /srv/hass/bin/hass $FLAGS ?
Normally i start hass with sudo -u hass -H /srv/hass/bin/hass

Or is it

CMD="$PRE_EXEC hass $FLAGS $REDIRECT;"

i should change to

CMD="$PRE_EXEC sudo -u hass -H /srv/hass/bin/hass $FLAGS $REDIRECT;

I havenĀ“t been home for a while and iĀ“m still not home so i havenĀ“t been able to try it.

Here is what I have in my config. I ended up changing to calling a shell with sudo, and then passing in the commands to start hass.

I had trouble with the redirect, so havenā€™t changed it to use the FLAGs variable. The > redirect will not work with sudoā€¦as in it will end up going into a file owned by root. Bascially, sudo takes the command all the way up to the >. The > then acts like a normal redirect, but under the root user. To redirect to a file owned by hass, you need to use tee.

sudo -u hass -H bash -c "/srv/hass/bin/hass -v --config $CONFIG_DIR --pid-file /var/run/hass.pid --daemon | tee -a $CONFIG_DIR/home-assistant.log"

So essence, this is calling sudo to run a bash shell under the hass user, passing the commands to run hass to the called shell with the pipe to tee for the log file.

So does this replace the FLAGS line? I am a little lost as to where you put this.

This is what my script looks like up until the start function. Meaning I only copied pasted the relevant parts.

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
  sudo -u hass -H bash -c "/srv/hass/bin/hass $FLAGS | tee -a $CONFIG_DIR/home-assistant.log"
  echo 'Service started' >&2
}

Just make sure you do the install before running startā€¦ie service hass-daemon install

I must be doing something wrong because I can get this to start from the command line however when I try to get this to start from boot it wonā€™t work. I also canā€™t seem to get the log file to work so I canā€™t determine what is wrong. I am not using a virtualenv but it should still work. The following is what my config looks like.

PRE_EXEC=""
RUN_AS="mgranger"
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 hass $FLAGS $REDIRECT;"
#  su -c "$CMD" $RUN_AS
sudo -u mgranger -H bash -c "hass $FLAGS | tee -a $CONFIG_DIR/home-assistant.log"
echo 'Service started' >&2 }

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.