0

Tried executing the .sh file present in the remote server with below code.

     String command1="/home/support/test.sh";
        try{

            java.util.Properties config = new java.util.Properties(); 
            config.put("StrictHostKeyChecking", "no");
            JSch jsch = new JSch();
            Session session=jsch.getSession(user, host, 22);
            session.setPassword(password);
            session.setConfig(config);
            session.connect();
            System.out.println("Connected");

            Channel channel=session.openChannel("exec");
             ((ChannelExec) channel).setCommand(command1);

            ((ChannelExec) channel).setErrStream(System.err);


            channel.connect();






            channel.disconnect();
            session.disconnect();

Content in shell script:

     flname="12.log"
     yest=`head -1 yes.txt`
     filename=$flname.$yest

    filename3=${filename}_prod3


  scp  user@82.xx.xx.xx:/app/12/log/${filename}           
  /home/support/${filename}
    mv ${filename} ${filename3}

   today=$( date +"%Y-%m-%d")

    java -jar /home/hybris/support/timeoutRprt.jar  /home/support/${filename3} 

After executing the script present in the remote server from the java class in my desktop, it doesn't create the files(output of jar file present in script) in remote server. But if I manually execute the script through putty, it produces the required files.

What might be case here?

knix2
  • 327
  • 1
  • 5
  • 19
  • You have not provide enough information. Any output? Any exception? What have you done to debug the problem? There are so many moving parts here that figuring out what's wrong will require a lot more work and access to things you have not provided. – Jim Garrison Jul 21 '14 at 17:27
  • Output from java: Connected DONE . Manualy executing the .sh file generates a .log file and excel file as output – knix2 Jul 21 '14 at 17:36
  • Have you tried stepping through the code in a debugger? That's the place to start. – Jim Garrison Jul 21 '14 at 17:37
  • I added this: `InputStream in=channel.getInputStream(); channel.connect(); byte[] tmp=new byte[1024]; while(true){ while(in.available()>0){ int i=in.read(tmp, 0, 1024); if(i<0)break; System.out.print(new String(tmp, 0, i)); } if(channel.isClosed()){ System.out.println("exit-status: "+channel.getExitStatus()); break; } try{Thread.sleep(1000);}catch(Exception ee){} }` – knix2 Jul 21 '14 at 17:41
  • Output: head: cannot open `yes.txt' for reading: No such file or directory scp: /app/12/log/form.log.: No such file or directory mv: cannot stat `form.log.': No such file or directory Error: /home/support/form.log._prod3 (No such file or directory) – knix2 Jul 21 '14 at 17:43

1 Answers1

1

Are you sure they're running from the same directory when run under JSCH and PuTTY? The fact that it can't find yes.txt in one case but it can in the other seems to point to a difference in your local directory. Are you using the same login for JSCH and PuTTY?

Tim
  • 2,027
  • 15
  • 24
  • Are you saying java class is trying to find the yes.txt in my local system ? All the files are present in the remote system. The directory and login are same. – knix2 Jul 21 '14 at 17:48
  • 1
    Clearly the files are present in the remote system, or you wouldn't be able to run from PuTTY. But that doesn't mean that PuTTY and JSCH are necessarily using the same working directory. You might want to echo out the current directory (found by executing `pwd` in your script) to make sure you're really running where you think you are. – Tim Jul 21 '14 at 17:59
  • Thanks ..Changed the directory..! – knix2 Jul 22 '14 at 11:08