View previous topic :: View next topic |
Author |
Message |
ascendant n00b
Joined: 13 Nov 2008 Posts: 60 Location: / (USA)
|
Posted: Mon Jul 05, 2021 12:15 am Post subject: OpenHAB on Gentoo |
|
|
Hi all,
I thought I'd share my openHAB setup in case someone wants to copy it. It is configured to run from a custom install location, but could be adapted to use /opt/openhab if you want to do that.
/etc/init.d/openhab: | #!/sbin/openrc-run
command=/mnt/media/bin/installs/openhab/runtime/bin/start
command_args_background=daemon
command_user=openhab
pidfile=/var/run/openhab.pid
name=openHAB
directory=/mnt/media/bin/installs/openhab
#be more patient on shutdown
retry=30
depend() {
need openvpn
}
start_pre() {
getfacl --omit-header --skip-base --absolute-names /run/lock |
grep --quiet user:$command_user:rwx
if [[ $? == 1 ]]
then
einfo "Granting $command_user access to /run/lock"
setfacl --modify u:$command_user:rwx /run/lock
fi
}
start_post() {
success=1
for i in eval echo {1..$retry}
do
pid=$(awk --field-separator " = " \
'$1 == "item.0.pid" {print $2}' \
/home/$command_user/data/tmp/instances/instance.properties)
if [[ $(ps --no-headers --format user "${pid}" 2>/dev/null) == $command_user ]]
then
success=0
break
else
sleep 1
fi
done
if [[ $success == 0 ]]
then
echo "${pid}" > $pidfile
fi
eend $success
} |
/etc/conf.d/openhab: | export OPENHAB_HTTP_ADDRESS=....
export OPENHAB_HTTP_PORT=....
export OPENHAB_HTTPS_PORT=....
export OPENHAB_USERDATA=/home/openhab/data
export JAVA_HOME=/opt/openjdk-bin-11
export JAVA=${JAVA_HOME}/bin/java |
The ACL modification of /run/lock is there due to openhab needing to write a lock file when interacting with a USB serial Z-Wave device. If you're not using that, you can remove start_pre()
This assumes:
- you've created an openhab user
- placed openhab in the paths above
- created ~openhab/data
- are using openjdk-bin:11
- openhab listens on an OpenVPN interface instead of a public one
Probably one of the worst behaviors of OpenHAB is that they detect Gentoo on startup and use Gentoo's java-config, which is incompatible with Java 11, which OpenHAB in turn depends on (all instead of simply respecting JAVA_HOME). _________________ This post brought to you by a cheap router. |
|
Back to top |
|
|
Hu Administrator
Joined: 06 Mar 2007 Posts: 21724
|
Posted: Mon Jul 05, 2021 4:27 pm Post subject: Re: OpenHAB on Gentoo |
|
|
ascendant wrote: | /etc/init.d/openhab: | pidfile=/var/run/openhab.pid |
| Could this be /run/openhab.pid now, since /var/run is a symlink to /run? ascendant wrote: | /etc/init.d/openhab: | start_pre() {
getfacl --omit-header --skip-base --absolute-names /run/lock |
grep --quiet user:$command_user:rwx
if [[ $? == 1 ]] |
| I think this could be moved into the if test: Code: | if ! getfacl --omit-header --skip-base --absolute-names /run/lock | grep -qF "user:$command_user:rwx" |
ascendant wrote: | /etc/init.d/openhab: | for i in eval echo {1..$retry} |
| I don't think this does what you intended. Code: | $ for i in eval echo {1..$a}; do echo i=$i; done
i=eval
i=echo
i={1..5} | Brace expansion occurs too early in the processing, so the sequence still has the placeholder variable name and does not become a sequence of numbers. Your eval does not reorder anything. Adding backticks or $( ) would fix the ordering, but unfortunately would still require eval. Code: | $ for i in `eval echo {1..$a}`; do echo i=$i; done
i=1
i=2
i=3
i=4
i=5 | You could also use the open-coded form: Code: | i=0
while [[ "$i" -lt "$retry" ]]; do
(( ++ i ))
|
ascendant wrote: | /etc/init.d/openhab: | pid=$(awk --field-separator " = " \
'$1 == "item.0.pid" {print $2}' \ |
| If you can guarantee there is only one such line, you could have awk exit immediately after finding it, without reading the rest of the file. |
|
Back to top |
|
|
ascendant n00b
Joined: 13 Nov 2008 Posts: 60 Location: / (USA)
|
Posted: Mon Jul 05, 2021 6:55 pm Post subject: |
|
|
Thanks, I applied your recommendations and great catch on the {1..$retry}
/etc/init.d/openhab: | #!/sbin/openrc-run
name=OpenHAB
command=/mnt/media/bin/installs/openhab/runtime/bin/start
command_args_background=daemon
command_user=openhab
pidfile=/run/openhab.pid
directory=/mnt/media/bin/installs/openhab
#be more patient on shutdown
retry=30
depend() {
need openvpn
}
start_pre() {
if ! getfacl --omit-header --skip-base --absolute-names /run/lock |
grep --quiet user:$command_user:rwx
then
einfo "Granting $command_user access to /run/lock"
setfacl --modify u:$command_user:rwx /run/lock
fi
}
start_post() {
success=1
for i in $(eval echo {1..$retry})
do
pid=$(awk --field-separator " = " \
'$1 == "item.0.pid" {print $2; exit}' \
"${OPENHAB_USERDATA}/tmp/instances/instance.properties")
if [[ $(ps --no-headers --format user "${pid}" 2>/dev/null) == $command_user ]]
then
success=0
break
else
sleep 1
fi
done
if [[ $success == 0 ]]
then
echo "${pid}" > $pidfile
fi
eend $success
} |
It still works
The most difficult thing writing this was really the "#be more patient on shutdown" section. I just could not get it to wait more than a couple seconds, and I still don't really understand what I did to get that working.
I guess I should add that this was all written for OpenHAB 3.1. _________________ This post brought to you by a cheap router. |
|
Back to top |
|
|
|