Always get -1 exit status

Help
2008-05-03
2013-05-28
  • Barry Marcus

    Barry Marcus - 2008-05-03

    Hello,

    The method I wrote to execute commands on my linux machine always returns an exit status of -1.  It makes no difference what the command is.  I am a complete newbie to the Jsch package, so I'm at a complete loss to figure out what's going on.  Hopefully this is a very simple issue...  Any help would be appreciated.  Thanks in advance.

    I have pasted the method below.

    Barry

    -------------------------------------------------------------------
    public int executeCommand(String[] args, ConnectionInfo userInfo) {   
        int exitStatus = 0;
        if(args.length == 0) {return exitStatus = 1000;}
        if(userInfo == null) {return exitStatus = 1001;}
        if(userInfo.validateWithPassword() != 0) {return exitStatus = 1002;}

        try {
          String user = userInfo.getUserName();
          String host = userInfo.getHostName();
          int portNumber = userInfo.getPortNumber();
         
          //args[0] will hold the path to the command.sh or just the command to execute itself
          StringBuffer command = new StringBuffer();
          command.append(args[0]);
         
          //Get the possible command params to add on to the command to execute
          int argsLenth = args.length;
          while (argsLenth > 1) {
              int index = argsLenth -1;
              command.append(" ");
              command.append(args[index]);
              argsLenth --;
          }
          JSch jsch = new JSch();
          Session session = jsch.getSession(user, host, portNumber);
         
          // username and password will be given via UserInfo interface.
          session.setUserInfo(userInfo);
          session.connect();

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

          channel.connect();
          exitStatus = channel.getExitStatus();
          session.disconnect();
         
        } catch(Exception e){
          System.out.println("Exception - Exit Status = " + exitStatus + "\nException: " + e.toString());
          return exitStatus;
        }
        return exitStatus;
      }

     
    • Ray

      Ray - 2008-05-29

      I'm having the same problem. I am using the ant SSHExec task and the task always fails because of exit code 1. I set up a project to debug this through my IDE and it seems to me that there are two lines in Session.java that could set the exit code. Neither of these are ever run. Please, any thoughts on this?

      Thanks,
      Ray

       
    • cytoe

      cytoe - 2008-07-08

      You need to loop until the command completes. In the loop you can do:

      if (channel.isClosed())   {
            System.out.println(channel.getExitStatus());
            break;
      }

      i.e. the channel has to be closed before getExitStatus() is called.

       

Log in to post a comment.