5

I'm trying to set JAVA_HOME for elasticsearch but no luck till now.

I tried to set it in .bashrc, etc/environment, etc/.profile all fail.

this is the command I use to run elasticsearch:

sudo /etc/init.d/elasticsearch start

I tried debug the JAVA_HOME variable in terminal like this:

  1. echo $JAVA_HOME
  2. sudo echo $JAVA_HOME

I got the result /home/mockie/softwares/jdk1.8.0_45 for the both which is correct path for my JAVA.

I also tried debug /etc/init.d/elasticsearch like this:

echo "$JAVA_HOME/dodol"
exit 1

and the result was empty and only return "/dodol".

this is full code for etc/init.d/elasticsearch : https://gist.github.com/mockiemockiz/c9547aee791ee04c2e1d

and this is my etc/environment:

    PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games"

    JAVA_HOME=/home/mockie/softwares/jdk1.8.0_45

but when I try this :

$ sudo su
$ /etc/init.d/elasticsearch start

and it works! but what I want is to use sudo /etc/init.d/elasticsearch start without sudo su first. is it possible?

26
  • After set in /etc/enviroment, you reboot the system?
    – Roknauta
    Jul 2, 2015 at 17:08
  • I havent tried it, btw what the different with etc/environment and etc/.bashrc? is it correct home/mockie/.bashrc is just for my profile?
    – Kakashi
    Jul 2, 2015 at 17:16
  • If your issue is solved, please put up an answer explaining how did you solve it..that will be helpful for future readers..also the answer given already seems legit to me apart from the fact that you don't have to restart the system..you can also select the answer as accepted so that this issue can be marked as solved..
    – heemayl
    Jul 2, 2015 at 18:37
  • but what I'm asking is I want to use sudo /etc/init.d/elasticsearch start without sudo su first. is it possible?
    – Kakashi
    Jul 2, 2015 at 18:41
  • After setting JAVA_HOME in /etc/environment do source /etc/environment and then check..
    – heemayl
    Jul 2, 2015 at 18:43

4 Answers 4

5

The problem turned out to be the SysV script /etc/init.d/elasticsearch itself.

In the script the PID_DIR variable is set as :

PID_DIR=/var/run/elasticsearch 

but there is no such directory exists and there is command to create it in the script too.

The NAME and PID_FILE are set as:

NAME=elasticsearch
PID_FILE="$PID_DIR/$NAME.pid" 

So when the PID_FILE is trying to create a file "$PID_DIR/$NAME.pid" (/var/run/elasticsearch/elasticseach.pid) in $PID_DIR (/var/run/elasticsearch/), it is getting:

touch: cannot touch ‘/var/run/elasticsearch/elasticsearch.pid’: No such file or directory 

error as the directory /var/run/elasticsearch does not exists already.

About the JAVA_HOME variable, the script /etc/init.d/elasticsearch is not using the system's variable rather using/creating its own version of the variable that is well defined in the script.

According to the script, if JAVA_HOME is not set in /etc/default/elasticsearch it will try to set it manually by searching for certain files in certain directories, otherwise it will left it blank.

3
  • @Kakashi Glad i could help :)
    – heemayl
    Jul 2, 2015 at 21:44
  • according to this github.com/elastic/elasticsearch/issues/11594 this issue is a bug. and as you pointing out the problem above I just need to change PID_DIR to /var/run in /etc/default/elasticsearch so it doesn't need to create elasticsearch directory anymore. I'm still wondering why it worked at the first time with sudo su :( that was why i tought it's permission problem or something. at least I learned some bash command from this.
    – Kakashi
    Jul 3, 2015 at 19:27
  • @Kakashi Yeah.. i was pondering that too..hard to reach a colclusion now though..
    – heemayl
    Jul 3, 2015 at 19:29
2

I had the same problem, what I did was to create a /etc/default/elastic file with the next line inside:

JAVA_HOME=/pathto/jdk
1

As mentioned here EnvironmentVariables

You can set system-wide environmental variables with three ways:

  • /etc/environment
  • /etc/profile
  • /etc/profile.d/*.sh

You could use for example /etc/profile. Execute this on your machine

sudo echo "JAVA_HOME=/home/mockie/softwares/jdk1.8.0_45" >> /etc/profile
5
  • profile or .profile and which one the correct name? most of answer out there it always started with a dot.
    – Kakashi
    Jul 2, 2015 at 18:55
  • i try edit etc/profile like this: export JAVA_HOME=/home/mockie/softwares/jdk1.8.0_45 export PATH=$PATH:$HOME/bin:$JAVA_HOME/bin and still error
    – Kakashi
    Jul 2, 2015 at 18:59
  • @Kakashi Do not forget / at the beginning. Jul 2, 2015 at 19:12
  • sorry i mean /etc/profile and still doesnt work. i already checked it in history command and what I edited is /etc/profile with slash at the beginning and doesnt work.
    – Kakashi
    Jul 2, 2015 at 19:15
  • Do you have the folder profile.d? If not create it and create a new file in it with .sh extension and try to add export for JAVA_HOME inside it. Try to restart the server too. Jul 2, 2015 at 19:54
0

I had the problem, the solutions provided above probably work. however in case anybody wants to do the thing without restarting the server having just installed java, you can do what I did (which is probably wrong, but it worked).

Modify:

/etc/systemd/system/multi-user.target.wants/elasticsearch.service

add this line:

Environment=JAVA_HOME=/usr/lib/jvm/jdk-11.0.4/

in the [service] section You may have to replace /usr/lib/jvm/jdk-11.0.4/ with your actual java installation.

I hope this helps, and I apologise to the elasticsearch team for modifying their files, clearly I have no business modifying default files.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .