-1

I have some Unix shell scripts in one of our servers and I have written a Java program using Spring Boot which is deployed in another application server.From my Spring Boot application, I am remotely executing the shell script at the other server. My program is as below:

public int execute(String scriptName, String environemntVariable,
            String serverIp, String username, String password) throws Exception {
        Session session = null;
        ChannelExec channelExec = null;
        InputStream in = null;
        BufferedReader reader = null;
        List<String> result = new ArrayList<String>();
        int exitStatus = 0;
        try {
            JSch jsch = new JSch();
            session = jsch.getSession(username, serverIp);
            session.setConfig("StrictHostKeyChecking", "no");
            session.setPassword(password);
            session.connect();
            channelExec = (ChannelExec) session.openChannel("exec");
            in = channelExec.getInputStream();
            channelExec.setCommand(environemntVariable + " " + scriptName);
            channelExec.connect();

            reader = new BufferedReader(new InputStreamReader(in));
            String line;
            while ((line = reader.readLine()) != null) {
                LOGGER.info(line);
                result.add(line);
            }
            exitStatus = channelExec.getExitStatus();
            LOGGER.info("exitStatus returned: " + exitStatus);
        } catch(Exception e) {
            LOGGER.info("Error occurred while running the script: \n", e);
            exitStatus = 1;
        } finally {
            if (reader != null) {
                reader.close();
            }
            if (in != null) {
                in.close();
            }
            if (channelExec != null) {
                channelExec.disconnect();
            }
            if (session != null) {
                session.disconnect();
            }
        }
        return exitStatus;
    }

The problem is for some of the shell scripts, an exit status of -1 is being returned from the shell script. I have found in the documentation that on successful completion, exit status returned is 0 and for unsuccessful execution, exit status returned would be greater than 0. Can someone please guide me what does exit status -1 signify? As The shell scripts are being executed successfully but exit status returned is -1.

Anirban
  • 925
  • 4
  • 24
  • 54
  • For some scripts I had got an exit status of 255 which was fixed. For some I am getting -1. Can you please say me what does exit status -1/255 signify? – Anirban Jun 05 '20 at 05:29

1 Answers1

1

Have you ever tried to read the documentation?

Returns:
the exitstatus returned by the remote command, or -1, if the command not yet terminated (or this channel type has no command).

ceving
  • 21,900
  • 13
  • 104
  • 178
  • Sorry I had not read the documentation of jsch. So what is meant by command not yet terminated? When I call a script and the script, the control returns to my program from the script only after the script ends, right? Also what is meant by channel type has no command? – Anirban Jun 05 '20 at 05:49
  • Examples for commands, which will not terminate, are: `read` or `hexdump /dev/urandom` or `while sleep 1; do :; done` – ceving Jun 05 '20 at 05:56
  • The [constructor](https://epaul.github.io/jsch-documentation/javadoc/com/jcraft/jsch/ChannelExec.html#ChannelExec--) does not require a command argument. So it is possible to create `ChannelExec` objects, which do not have a command. – ceving Jun 05 '20 at 06:00
  • So can you please guide me in my above program, how will I come to know whether the script has executed successfully or not? Instead of checking an exit status as anything other than 0 for failure, will I check exit status of greater than 0 for failure? – Anirban Jun 05 '20 at 06:01
  • So I will change channelExec = (ChannelExec) session.openChannel("exec"); line to channelExec = (ChannelExec) session.openChannel(); right? – Anirban Jun 05 '20 at 06:03
  • This does not seem to be useful. – ceving Jun 05 '20 at 06:04
  • Can you please guide me what needs to be done here? – Anirban Jun 05 '20 at 06:09