Skip to content

Instantly share code, notes, and snippets.

@damoun
Last active June 26, 2018 19:07
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save damoun/96add58f60572cb12c11 to your computer and use it in GitHub Desktop.
Save damoun/96add58f60572cb12c11 to your computer and use it in GitHub Desktop.
home-assistant rc script for Freebsd
#!/bin/sh
# $FreeBSD$
#
# PROVIDE: hass
# REQUIRE: LOGIN
# KEYWORD: shutdown
#
# Add these lines to /etc/rc.conf.local or /etc/rc.conf
# to enable this service:
#
# hass_enable (bool): Set to NO by default.
# Set it to YES to enable home-assistant.
# hass_pidfile (path): Set to /var/run/home-assistant/hass.pid by default.
# hass_user (user): Set to hass by default.
# hass_config_dir (path): Set to /usr/local/etc/home-assistant/ by default.
# hass_logfile (path): Set to /var/log/home-assistant.log by default.
. /etc/rc.subr
name=hass
rcvar=${name}_enable
start_cmd=${name}_start
stop_cmd=${name}_stop
load_rc_config $name
: ${hass_enable:=NO}
: ${hass_pidfile:=/var/run/home-assistant/hass.pid}
: ${hass_user:=hass}
: ${hass_config_dir:=/usr/local/etc/home-assistant/}
: ${hass_logfile:=/var/log/home-assistant.log}
command=hass
command_args="-v --config $hass_config_dir --pid-file $hass_pidfile --daemon >>& $hass_logfile"
hass_start()
{
if [ -f $hass_pidfile ] && kill -0 $(cat $hass_pidfile) >/dev/null 2>&1 ; then
echo 'Service already running'
return 1
fi
echo 'Starting service'
su -m $hass_user -c "$command $command_args"
echo 'Service started'
}
hass_stop()
{
if [ ! -f $hass_pidfile ] || ! kill -0 $(cat $hass_pidfile) >/dev/null 2>&1 ; then
echo 'Service not running'
return 1
fi
echo 'Stopping service'
kill $(cat $hass_pidfile)
while ps -p $(cat $hass_pidfile) >/dev/null 2>&1 ; do sleep 1;done
echo 'Service stopped'
}
run_rc_command "$1"
@djtlg
Copy link

djtlg commented Mar 20, 2017

Hey I realized the script fails to start the Home Asistant if the /var/run/home-assistant/ is missing. It doesn't write anything to log as well but it give a false "Service Started" message. Though you might want to know. Thanks for the script.

@tantecky
Copy link

tantecky commented Mar 20, 2017

Moreover I think that redirection should be &>> instead of >>& and su might be without -m hence $HOME is properly set. Thank you for the gist!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment