Autostart Virtualenv Ubuntu 14.04

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