diff --git a/homeassistant/__main__.py b/homeassistant/__main__.py index f855b8f5496..e219b973f37 100644 --- a/homeassistant/__main__.py +++ b/homeassistant/__main__.py @@ -121,13 +121,16 @@ def check_pid(pid_file): try: pid = int(open(pid_file, 'r').readline()) except IOError: + # PID File does not exist pass else: try: os.kill(pid, 0) except OSError: + # PID does not exist pass else: + # PID already exists print('Fatal Error: HomeAssistant is already running.') sys.exit(1) diff --git a/scripts/hass-daemon b/scripts/hass-daemon new file mode 100644 index 00000000000..5206a446725 --- /dev/null +++ b/scripts/hass-daemon @@ -0,0 +1,97 @@ +#!/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 +# virutal environment), put them in PRE_EXEC. +# 2) Set RUN_AS to the username that should be used to execute hass. +# 3) Copy this script to /etc/init.d/ +# 4) Install this script +# sudo hass-daemon install +# 5) 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="" +RUN_AS="USER" +PID_FILE="/var/run/hass.pid" +CONFIG_DIR="/var/opt/homeassistant" +FLAGS="-v --config $CONFIG_DIR --pid-file $PID_FILE --daemon" + +start() { + if [ -f $PID_FILE ] && kill -0 $(cat $PID_FILE); then + echo 'Service already running' >&2 + return 1 + fi + echo 'Starting service…' >&2 + local CMD="$PRE_EXEC; hass $FLAGS;" + su -c "$CMD" $RUN_AS + echo 'Service started' >&2 +} + +stop() { + if [ ! -f "$PID_FILE" ] || ! kill -0 $(cat "$PID_FILE"); then + echo 'Service not running' >&2 + return 1 + fi + echo 'Stopping service…' >&2 + kill -3 $(cat "$PID_FILE") + echo 'Service stopped' >&2 +} + +install() { + echo "Installing Home Assistant Daemon (hass-daemon)" + touch $PID_FILE + chwon $RUN_AS $PID_FILE + mkdir -p $CONFIG_DIR + chown $RUN_AS $CONFIG_DIR + update-rc.d homeassistant defaults +} + +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 diff --git a/scripts/homeassistant-pi.sh b/scripts/homeassistant-pi.sh deleted file mode 100755 index 1d5537f0191..00000000000 --- a/scripts/homeassistant-pi.sh +++ /dev/null @@ -1,78 +0,0 @@ -#!/bin/sh - -# To script is for running Home Assistant as a service and automatically starting it on boot. -# Assuming you have cloned the HA repo into /home/pi/Apps/home-assistant adjust this path if necessary -# This also assumes you installed HA on your raspberry pi using the instructions here: -# https://home-assistant.io/getting-started/ -# -# To install to the following: -# sudo cp /home/pi/Apps/home-assistant/scripts/homeassistant-pi.sh /etc/init.d/homeassistant.sh -# sudo chmod +x /etc/init.d/homeassistant.sh -# sudo chown root:root /etc/init.d/homeassistant.sh -# -# If you want HA to start on boot also run the following: -# sudo update-rc.d homeassistant.sh defaults -# sudo update-rc.d homeassistant.sh enable -# -# You should now be able to start HA by running -# sudo /etc/init.d/homeassistant.sh start - -### BEGIN INIT INFO -# Provides: myservice -# Required-Start: $remote_fs $syslog -# Required-Stop: $remote_fs $syslog -# Default-Start: 2 3 4 5 -# Default-Stop: 0 1 6 -# Short-Description: Put a short description of the service here -# Description: Put a long description of the service here -### END INIT INFO - -# Change the next 3 lines to suit where you install your script and what you want to call it -DIR=/home/pi/Apps/home-assistant -DAEMON="/home/pi/.pyenv/shims/python3 -m homeassistant" -DAEMON_NAME=homeassistant - -# Add any command line options for your daemon here -DAEMON_OPTS="" - -# This next line determines what user the script runs as. -# Root generally not recommended but necessary if you are using the Raspberry Pi GPIO from Python. -DAEMON_USER=pi - -# The process ID of the script when it runs is stored here: -PIDFILE=/var/run/$DAEMON_NAME.pid - -. /lib/lsb/init-functions - -do_start () { - log_daemon_msg "Starting system $DAEMON_NAME daemon" - start-stop-daemon --start --background --chdir $DIR --pidfile $PIDFILE --make-pidfile --user $DAEMON_USER --chuid $DAEMON_USER --startas $DAEMON -- $DAEMON_OPTS - log_end_msg $? -} -do_stop () { - log_daemon_msg "Stopping system $DAEMON_NAME daemon" - start-stop-daemon --stop --pidfile $PIDFILE --retry 10 - log_end_msg $? -} - -case "$1" in - - start|stop) - do_${1} - ;; - - restart|reload|force-reload) - do_stop - do_start - ;; - - status) - status_of_proc "$DAEMON_NAME" "$DAEMON" && exit 0 || exit $? - ;; - *) - echo "Usage: /etc/init.d/$DAEMON_NAME {start|stop|restart|status}" - exit 1 - ;; - -esac -exit 0 diff --git a/scripts/homeassistant.daemon b/scripts/homeassistant.daemon deleted file mode 100755 index 4dd6b37a9c5..00000000000 --- a/scripts/homeassistant.daemon +++ /dev/null @@ -1,88 +0,0 @@ -#!/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= -RUNDIR= -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 - ;; - restart) - stop - start - ;; - *) - echo "Usage: $0 {start|stop|restart|uninstall}" -esac