CLOUDSTACK-4813: add function to get ExitValue when run bash commands

(cherry picked from commit b4397acfccd651bc67bb842097def5ded995c125)
This commit is contained in:
Wei Zhou 2013-10-04 11:05:41 +02:00
parent 5ed493c6e6
commit 3a999e70af

View File

@ -201,8 +201,8 @@ public class Script implements Callable<String> {
if (interpreter != null) {
return interpreter.drain() ? task.getResult() : interpreter.interpret(ir);
} else {
// null return is ok apparently
return (_process.exitValue() == 0) ? "Ok" : "Failed, exit code " + _process.exitValue();
// null return exitValue apparently
return String.valueOf(_process.exitValue());
}
} else {
break;
@ -245,7 +245,7 @@ public class Script implements Callable<String> {
error = interpreter.processError(reader);
}
else {
error = "Non zero exit code : " + _process.exitValue();
error = String.valueOf(_process.exitValue());
}
if (_logger.isDebugEnabled()) {
@ -482,4 +482,26 @@ public class Script implements Callable<String> {
return result.trim();
}
public static int runSimpleBashScriptForExitValue(String command) {
return runSimpleBashScriptForExitValue(command, 0);
}
public static int runSimpleBashScriptForExitValue(String command, int timeout) {
Script s = new Script("/bin/bash", timeout);
s.add("-c");
s.add(command);
String result = s.execute(null);
if (result == null || result.trim().isEmpty())
return -1;
else {
try {
return Integer.valueOf(result.trim());
} catch (NumberFormatException e) {
return -1;
}
}
}
}