textproc/elasticsearch6 does not honour /etc/profile

It looks like textproc/elasticsearch6 does not honour /etc/profile. To reproduce the issue:
Bash:
root@FreeBSD [~]# cd /usr/ports/java/openjdk8
make install clean

root@FreeBSD [~]# cd /usr/ports/textproc/elasticsearch6
make install clean
Bash:
root@FreeBSD [~]# cat << EOF > /etc/profile
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin:${HOME}/bin; export PATH
JAVA_HOME=/usr/local/openjdk8; export JAVA_HOME
CLASSPATH=.:${JAVA_HOME}/jre/lib/rt.jar:${JAVA_HOME}/jre/lib/javazic.jar; export CLASSPATH
EOF
root@FreeBSD [~]# chmod 0644 /etc/profile

Bash:
root@FreeBSD [~]# vi /usr/local/etc/elasticsearch/elasticsearch.yml
[...]
network.host: ["127.0.0.1", "[::1]"]
http.port: 9200
[...]

root@FreeBSD [~]# service elasticsearch start
Starting elasticsearch.
could not find java; set JAVA_HOME or ensure java is in PATH
/usr/local/etc/rc.d/elasticsearch: WARNING: failed to start elasticsearch

Bash:
root@FreeBSD [~]# env
[...]
SSH_CONNECTION=[...]
USER=root
JAVA_HOME=/usr/local/openjdk8
CLASSPATH=.:/usr/local/openjdk8/jre/lib/rt.jar:/usr/local/openjdk8/jre/lib/javazic.jar
SHLVL=1
LOGNAME=root
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin:/root/bin
[...]

For debugging, I put echo -e "Environment:\n$(env)" in /usr/local/lib/elasticsearch/bin/elasticsearch-env
Bash:
root@FreeBSD [~]# vi /usr/local/lib/elasticsearch/bin/elasticsearch-env
[...]
echo -e "Environment:\n$(env)"
if [ ! -x "$JAVA" ]; then
  echo "could not find java; set JAVA_HOME or ensure java is in PATH"
  exit 1
fi
[...]

root@FreeBSD [~]# echo 'rc_debug="YES"' >> /etc/rc.conf

root@FreeBSD [~]# ls -lach /usr/local/openjdk8/bin/java
-r-xr-xr-x  1 root  wheel   7.7K Mar 27 00:26 /usr/local/openjdk8/bin/java

root@FreeBSD [~]# service elasticsearch start
/usr/local/etc/rc.d/elasticsearch: DEBUG: pid file (/var/run/elasticsearch/elasticsearch.pid): not readable.
/usr/local/etc/rc.d/elasticsearch: DEBUG: checkyesno: elasticsearch_enable is set to YES.
/usr/local/etc/rc.d/elasticsearch: DEBUG: run_rc_command: start_precmd: elasticsearch_precmd
Starting elasticsearch.
/usr/local/etc/rc.d/elasticsearch: DEBUG: run_rc_command: doit:  limits -C root su -m elasticsearch -c 'sh -c "/usr/local/lib/elasticsearch/bin/elasticsearch  -d --pidfile=/var/run/elasticsearch/elasticsearch.pid"'
Environment:
RC_PID=34362
ES_PATH_CONF=/usr/local/etc/elasticsearch
PWD=/
HOME=/
SHLVL=2
PATH=/sbin:/bin:/usr/sbin:/usr/bin
_=/usr/bin/env
could not find java; set JAVA_HOME or ensure java is in PATH
/usr/local/etc/rc.d/elasticsearch: WARNING: failed to start elasticsearch

So it is pretty clear that environment variable ${JAVA_HOME} from /etc/profile did not get honoured. A quick grep -r showed me a potential source of were the ${HOME} and ${PATH} could have been set to these values above:
Bash:
root@FreeBSD [~]# grep -r "/sbin:/bin:/usr/sbin:/usr/bin" / 2>/dev/null
/usr/sbin/service:              exec env -i HOME=/ PATH=/sbin:/bin:/usr/sbin:/usr/bin $dir/$script $*

textproc/elasticsearch6's MAKEFILE creates the user elasticsearch like this (which should be fine in my opinion):
Bash:
getent passwd | grep elasticsearch
elasticsearch:*:965:965:elasticsearch user:/nonexistent:/usr/sbin/nologin

My current dirty workarround is to add JAVA_HOME=/usr/local/openjdk8 into /usr/local/lib/elasticsearch/bin/elasticsearch-env:
Bash:
root@FreeBSD [~]# vi /usr/local/lib/elasticsearch/bin/elasticsearch-env
[...]
JAVA_HOME=/usr/local/openjdk8
# now set the path to java
if [ -x "$JAVA_HOME/bin/java" ]; then
  JAVA="$JAVA_HOME/bin/java"
else
  set +e
  JAVA=`which java`
  set -e
fi
[...]

Any idea what causes this unexpected behaviour and how to fix it sustainable?
 
Last time I installed ElasticSearch it just worked out of the box. Why are you messing around with JAVA_HOME and all?
 
Likely because right out of the box (at least for myself installing textproc/elasticsearch6 from pkg), it fails to start and prompts with the following:

Code:
# service elasticsearch start
/usr/local/etc/rc.d/elasticsearch: DEBUG: pid file (/var/run/elasticsearch/elasticsearch.pid): not readable.
/usr/local/etc/rc.d/elasticsearch: DEBUG: checkyesno: elasticsearch_enable is set to YES.
/usr/local/etc/rc.d/elasticsearch: DEBUG: run_rc_command: start_precmd: elasticsearch_precmd
Starting elasticsearch.
/usr/local/etc/rc.d/elasticsearch: DEBUG: run_rc_command: doit:  limits -C root su -m elasticsearch -c 'sh -c "/usr/local/lib/elasticsearch/bin/elasticsearch  -d --pidfile=/var/run/elasticsearch/elasticsearch.pid"'
could not find java; set JAVA_HOME or ensure java is in PATH
/usr/local/etc/rc.d/elasticsearch: WARNING: failed to start elasticsearch
 
Looks like sysutils/logstash6 may have already dealt with this (or a similar) issue?
# cat /usr/local/etc/rc.d/logstash
Code:
[...]
# logstash_java_home (string):
#   Default value: "/usr/local/openjdk8"
#   Root directory of the desired Java SDK.
#   The JAVA_HOME environment variable is set with the contents of this
#   variable.
#
# logstash_java_opts (string):
#   Default value: ""
#   Options to pass to the Java Virtual Machine.
#   The JAVA_OPTS environment variable is set with the contents of this
#   variable.
#
[...]
JAVA_OPTS="${logstash_java_opts}"
JAVA_HOME="${logstash_java_home}"
export JAVA_OPTS
export JAVA_HOME
[...]
Maybe I need to send this suggestion along to the maintainer of textproc/elasticsearch6
 
There's no need to modify anything. Over the weekend I installed textproc/elasticsearch6 and it worked right away. There was no need to modify anything. I suggest you remove everything you added to /etc/profile as it's likely interfering.
 
There's no need to modify anything. Over the weekend I installed textproc/elasticsearch6 and it worked right away.
I find this on a fresh install as well and it starts without issue.

However, you change the root shell with chsh -s to a shell other than /bin/csh (the default) or /bin/tcsh, textproc/elasticsearch6 then fails to find java. I ran into this last night trying to troubleshoot the cause, and I often change my working shells (both user and root) to bash.

For some reason, changing the root shell changes the $PATH being passed to /usr/local/lib/elasticsearch/bin/elasticsearch such that java is no longer found.

I can reproduce this on fresh installations with both:
# chsh -s /bin/sh
…and…
# chsh -s /usr/local/bin/bash
 
I find this on a fresh install as well and it starts without issue.

However, you change the root shell with chsh -s to a shell other than /bin/csh (the default) or /bin/tcsh, textproc/elasticsearch6 then fails to find java. I ran into this last night trying to troubleshoot the cause, and I often change my working shells (both user and root) to bash.

For some reason, changing the root shell changes the $PATH being passed to /usr/local/lib/elasticsearch/bin/elasticsearch such that java is no longer found.

I can reproduce this on fresh installations with both:
# chsh -s /bin/sh
…and…
# chsh -s /usr/local/bin/bash
This is exactly the case. My default shell of root and other users is bash. The issue is still present.
 
Back
Top