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