1

i have my code something like this the script "script.sh" takes some 7 minutes to execute completely

private void function(String par1,String par2,String par3,String par4){
        String logFile = logFolder + par1 + "_label.log";
        String cmd = " /bin/sh " + scriptFolder + "script.sh" + " " +
                par1 + " "
                + par2 + " "
                + par3 + " "
                + par4 + " > " +
                logFile + " 2>&1 ";
        logger.info("label update script" +cmd);
        /*  vm info */

        String host = ConstantsServers.LABEL_UPDATE_SERVER;
        String user = "USER";
        String passwd = System.getenv("USER_PW");
        /*
         * start the script as user on the server
         */
        new RunCmdViaSSH(host, user, passwd, cmd);
        logger.debug("Log file created at:" +logFile);

    }

this code executes the shell script and stores the output in a log file. how do i check in java whether this shell script ran successfully or not. please suggest.

i tried checking the last line of the file using a file reader with a code something like this

try (BufferedReader br = new BufferedReader(new FileReader(file))) {

                            //checks whether the script ran successfully or not
                            String strLine = null,tmp;
                            while ((tmp = br.readLine()) != null) {
                                strLine=tmp;
                            }
                            String lastLine = strLine;
                            logger.debug(lastLine);
                            if (lastLine.contains("script ran successfully")) {
                                  logger.debug("script passed");
                            }
                            else{
                                logger.debug("script failed");
                             }

but the problem with this code is that it will nit wait untill the log file is completely updated,it will just read the first few lines and print the else part.

is there any other way to do this? please suggest

noob_coder
  • 749
  • 4
  • 15
  • 35
  • Probably you should wait until `RunCmdViaSSH` actually finishes. Unfortunately, i could not find any information about `RunCmdViaSSH`. – kgeorgiy Nov 08 '16 at 07:50

3 Answers3

2

Best way to deal with this is to use the ProcessBuilder API which will give you control over the execution. And you can leverage its Process API to wait your processing till it completes execution and will also return the exit code.(Synchronous Processing)

Sample of how to use this API is below

ProcessBuilder pb = new ProcessBuilder("script.sh", arg1, arg2);
Process p = pb.start();
int exitCode = p.waitFor();

You need to check if the process terminated which you can do with p.waitFor() which blocks until the process completed. The return value of this call is the return code of the system command you invoked.

If your exitCode value is 0, it means successfully executed.

Best fit for your need as it

Causes the current thread to wait, if necessary, until the process represented by this Process object has terminated.

mhasan
  • 3,703
  • 1
  • 18
  • 37
  • but how do i store the output of my shell script into a log file using this API. – noob_coder Nov 08 '16 at 09:46
  • This link should get you going for this http://stackoverflow.com/questions/8230963/how-to-output-logs-from-a-shell-script-launched-within-java-in-a-log4j-rollingfi – mhasan Nov 08 '16 at 09:59
0

One way to do that is to execute a .jar at the end of script and in that .jar you can check either it completed successful or not, or you can also hit URL (if have any) to notify the other component at the end of script. depends on your scenario.

Umais Gillani
  • 608
  • 4
  • 9
0

You can make your ssh connection via java and than execute your commands using this connection. And like running commands via Java on your own machine, you can run your commands on this connection. This way you can use

Process process = Process.getRuntime().exec(sshCmd);
// you should get error stream and check for errors before waiting
process.getOutputStream().println(yourCmd);
process.waitFor();
// now process is finished and you can get full output from process.getInputstream()

Have a look at bobah's answer.

You can automate ssh login with sshpass and I think there are some other ways for doing it as well. Search google for "ssh with password".

Community
  • 1
  • 1
codanza
  • 33
  • 1
  • 6