Search code examples
linuxbashspring-bootjschexitstatus

Exit status -1 from a Linux shell script


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.


Solution

  • 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).