1

I have created a RHEL Linux VM and installed Oracle JDK in it.

Post creation of VM, I am executing script using Custom Script extension in VM. Below is my script.

Myscript.sh

echo "$$ $JAVA_HOME $$" >> output.log
echo `env` >> output.log

output.log

$$ $$
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin PWD=/var/lib/waagent/custom-script/download/1 LANG=en_US.UTF-8 SHLVL=4 _=/usr/bin/env

When I ran the above script, I don't see JAVA_HOME is set. But I see JAVA_HOME value when I logged into the machine.

Update:-

During installation of Oracle JDK, I set JAVA_HOME in /etc/profile.

script1.sh

javaHomeValue=$(cat /etc/profile | grep JAVA_HOME | awk -F= '{print $2}')

echo " -- $javaHomeValue -- " >> output.log

output.log

-- /usr/java/jdk1.8.0_172-amd64 --

I am able to get JAVA_HOME using the above script1.sh

Is there any reason, Why JAVA_HOME is not set in environment variables during execution of Custom script?

Galet
  • 5,853
  • 21
  • 82
  • 148

1 Answers1

0

For your issue, if you want to set JAVA_HOME as environment variable in file /etc/profile, you can add code export JAVA_HOME=/usr/java/jdk1.8.0_172-amd64 into /etc/profile. Then you can see the environment variable JAVA_HOME in a new terminal.

Charles Xu
  • 29,862
  • 2
  • 22
  • 39
  • How would i add environment variable in scale set linux instances (auto scaling enabled)? – lambad Jun 05 '19 at 10:22
  • @lambad I will suggest you change the image with the environment variables already set in it. – Charles Xu Jun 06 '19 at 06:20
  • If I manually add environment variables in profile file and generalize and create image then all the added environment variables will be destroyed as a result I won't be able to access them. – lambad Jun 06 '19 at 06:50
  • @lambad When you generate the VM, it will delete the home directory, so you can set the environment variables in another path. I will suggest set them in the /etc/environment file. – Charles Xu Jun 06 '19 at 06:54