0

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.

Jose Antonio
  • 578
  • 1
  • 8
  • 34
  • If I understand the problem properly, the guess is that `JAVA_HOME` is an environment variable in one server, so you cannot assume it exists and is the same in the other one. – fedorqui Oct 31 '14 at 12:02
  • Are you assuming that JAVA_HOME defined in one server is passed to another when you do ssh? – SMA Oct 31 '14 at 12:05
  • Each machine has defined a specific JAVA_HOME. If I run the script2 (in machine2) from machine2, it works correctly. But If I run the script1 (in machine1) from machine1 that calls script2, the JAVA_HOME in script2 is lost (error JAVA_HOME not found). – Jose Antonio Oct 31 '14 at 12:18

1 Answers1

1

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();
Jose Antonio
  • 578
  • 1
  • 8
  • 34