From 33640d18af28acb410ccf43a881d86ba177bd525 Mon Sep 17 00:00:00 2001 From: Questler Date: Wed, 6 Dec 2017 22:41:27 +0100 Subject: [PATCH] Update init.d.markdown (#4147) * Update init.d.markdown Added code for Python virtual environment * Update init.d.markdown * Update init.d.markdown --- source/_docs/autostart/init.d.markdown | 91 ++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) diff --git a/source/_docs/autostart/init.d.markdown b/source/_docs/autostart/init.d.markdown index 9f67cbdb2b5..6605b6e10b9 100644 --- a/source/_docs/autostart/init.d.markdown +++ b/source/_docs/autostart/init.d.markdown @@ -142,3 +142,94 @@ case "$1" in echo "Usage: $0 {start|stop|restart|install|uninstall}" esac ``` + +### {% linkable_title Python virtual environment %} + +```bash +#!/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 +PRE_EXEC="cd /srv/homeassistant && python3 -m venv . && source bin/activate &&" +# Typically /usr/bin/hass +HASS_BIN="hass" +RUN_AS="USER" +PID_FILE="/var/run/hass.pid" +CONFIG_DIR="/home/USER/.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_BIN $FLAGS $REDIRECT;" + su -s /bin/bash -c "$CMD" $RUN_AS + 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 +```