core/scripts/homeassistant.daemon

89 lines
2.6 KiB
Bash
Executable File

#!/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 variables
# 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.
SCRIPT="source bin/activate; ./bin/python -m homeassistant"
RUNAS=<USER TO RUN SERVER AS>
RUNDIR=<LOCATION OF home-assistant DIR>
PIDFILE=/var/run/homeassistant.pid
LOGFILE=/var/log/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; $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