Search code examples
unixbatch-filejava-home

JAVA_HOME is lost in script on remote unix machine


I have a script (script1.sh) in a unix machine that calls a other script (script2.sh) in other unix remote machine:

ssh user@machine  /opt/.../script2.sh param1 param2

There is a trust relationship between both machines.

If I run the script2, it works correctly, but if I run the script1, it calls script2 but JAVA_HOME of script2 is lost. I know that I can fix by "set JAVA_HOME" in script2 but I prefer other solution that I don´t have to put the specific path of JAVA_HOME in each scripts that is called by script1 (script2, script3,...)

Any idea?

Regards.


Solution

  • I didn´t find the solution so I tried other way. The other way is thought Java.

    script1 calls a Java Application that is in the same machine. This Java Application connect with the other machine and calls script2 and catchs the response. The code would be:

     //With Try catchs
     //Create connection
     JSch jSSH = new JSch();
     Session session = jSSH.getSession(user, server, port);
     UserInfo ui = new SessionUser(password, null);//SessionUser implements UserInfo
     session.setUserInfo(ui);
     session.setPassword(password);
     session.connect();
    
     //Create channel:
     Channel channel = session.openChannel("shell");
    
     //Configure inputs and outputs of channel:
     OutputStream inputstream_for_the_channel = channel.getOutputStream();
     PrintStream commander = new PrintStream(inputstream_for_the_channel, true);
    
     channel.setOutputStream(null);
     channel.connect(100);
    
     //Command to execute
     commander.println("cd /.../scriptFolder; ./script2.sh param1 param2; exit");
     commander.flush();
    
     //System.out.println(channel.getExitStatus());
    
     InputStream outputstream_from_the_channel = channel.getInputStream();
     BufferedReader br = new BufferedReader(new InputStreamReader(outputstream_from_the_channel));
            String line = null;
    
    //Catch and print the response with a StringBuilder
    
    //close connection      
    channel.disconnect();
    session.disconnect();