mirror of https://github.com/apache/cloudstack.git
CLOUDSTACK-4813: add function to get ExitValue when run bash commands
This commit is contained in:
parent
40716daeed
commit
b4397acfcc
|
|
@ -207,8 +207,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;
|
||||
|
|
@ -251,7 +251,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()) {
|
||||
|
|
@ -488,4 +488,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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue