ansible: improve homeassistant startup script
authorKristof Provost <kp@FreeBSD.org>
Mon, 10 Feb 2025 21:48:45 +0000 (22:48 +0100)
committerKristof Provost <kp@FreeBSD.org>
Mon, 10 Feb 2025 21:48:45 +0000 (22:48 +0100)
Ensure that 'status' and 'stop' actually work.
This is still imperfect because we trust that the existence of the pidfile
matches the existence of the process and that's only usually true.

ansible/templates/homeassistant

index 8906dab..4e3f85b 100644 (file)
 name="homeassistant"
 rcvar=${name}_enable
 pidfile=/var/run/${name}.pid
-procname=/home/domotica/ha/bin/hass
-start_precmd="ha_prestart"
+ppidfile=/var/run/${name}-parent.pid
 
+ha_command=/home/domotica/ha/bin/hass
 command=/usr/sbin/daemon
-command_args=" -f -S -p ${pidfile} -u domotica ${procname} --ignore-os-check"
+command_args=" -f -S -p ${pidfile} -P ${ppidfile} -u domotica ${ha_command} --ignore-os-check"
+
+start_precmd="ha_prestart"
+status_cmd="ha_status"
+stop_cmd="ha_stop"
+
 
 ha_prestart()
 {
        chown -R domotica:domotica /home/domotica/ha
 }
 
+ha_status()
+{
+       if [ -n "`cat $pidfile`" ]; then
+               echo "${name} is running as pid `cat $pidfile`."
+       else
+               echo ${name} is not running.
+               return 1
+       fi
+}
+
+ha_stop()
+{
+       echo "Stopping PID: $(cat ${ppidfile})"
+       /bin/kill -9 $(cat ${ppidfile})
+       rm ${ppidfile}
+       echo "Stopping PID: $(cat ${pidfile})"
+       /bin/kill -15 $(cat ${pidfile})
+       rm ${pidfile}
+}
+
 load_rc_config $name
 run_rc_command $1