0

I have the below linux script(simplified):

RUN_JAVA=$(which java)
if [ -z $RUN_JAVA ]
then
 echo "No Java found!" >> /tmp/output.txt
else
 echo $RUN_JAVA >> /tmp/output.txt
fi

When I run it at the same host,runs perfectly,outputs to file:

/usr/java/default/bin/java

But when I run it from another host:

ssh ${user}@${hotname} "/tmp/testRemote.sh"

Then it can not find the Java installation on that host:

No Java found!

What could be the reason?

selman
  • 103
  • 3

1 Answers1

1

which doesn't check any potential location, only the locations in $PATH.

And Java is not found in the $PATH that gets set on the remote host.

The reason is that ssh user@host command does not create a login session, but an interactive session and bash (and possibly other shells too) will source different files to set up your environment based on the session type.

See the INVOCATION section of the bash manual for the exact differences, but probably you add /usr/java/default/bin/ to your PATH in a ~/.bash_profile where ssh user@host command doesn't load that file and only uses ~/.bashrc to set up the environment.

HBruijn
  • 77,029
  • 24
  • 135
  • 201
  • Thanks.Indeed, I have my java added to PATH in gloval bash_profile. Any quick workaround exist for this? – selman Dec 07 '17 at 17:55