# textproc/elasticsearch6 does not honour /etc/profile



## Leander (Mar 29, 2018)

It looks like textproc/elasticsearch6 does not honour /etc/profile. To reproduce the issue:

```
root@FreeBSD [~]# cd /usr/ports/java/openjdk8
make install clean

root@FreeBSD [~]# cd /usr/ports/textproc/elasticsearch6
make install clean
```


```
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
```


```
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
```


```
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

```
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:

```
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_):

```
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:

```
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?


----------



## Leander (Mar 30, 2018)

Any clue whats wrong here?


----------



## SirDice (Mar 30, 2018)

Last time I installed ElasticSearch it just worked out of the box. Why are you messing around with JAVA_HOME and all?


----------



## himay (Apr 6, 2018)

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:


```
# 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
```


----------



## SirDice (Apr 6, 2018)

Do you have java/javavmwrapper installed?


----------



## himay (Apr 6, 2018)

SirDice said:


> Do you have java/javavmwrapper installed?



Yes, that was installed automatically as a dependency of java/openjdk8 (which is a dependency of textproc/elasticsearch6).


----------



## himay (Apr 7, 2018)

Leander said:


> My current dirty workarround is to add  JAVA_HOME=/usr/local/openjdk8 into /usr/local/lib/elasticsearch/bin/elasticsearch-env:


Apparently another (completely non-invasive) way to inject this into the `rc.subr` environment is to append to the textproc/elasticsearch6 `rc` environment in `rc.conf`:

```
elasticsearch_env="JAVA_HOME=/usr/local/openjdk8"
```

#TIL (about extra `rc.subr` variable calls!)


----------



## himay (Apr 7, 2018)

Looks like sysutils/logstash6 may have already dealt with this (or a similar) issue?
`# cat /usr/local/etc/rc.d/logstash`

```
[...]
# 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…


----------



## SirDice (Apr 9, 2018)

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.


----------



## himay (Apr 9, 2018)

SirDice said:


> 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`


----------



## Leander (Nov 4, 2018)

himay said:


> 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.
> 
> ...


This is exactly the case. My default shell of root and other users is bash. The issue is still present.


----------



## himay (Nov 5, 2018)

Leander said:


> The issue is still present.


I don't think it will be changing any time soon.
https://lists.freebsd.org/pipermail/freebsd-elastic/2018-May/000070.html


> Changing the root shell is strongly discouraged as many assumptions are made about which shell your root user has.


----------

