Skip to content

Commit b4397ac

Browse files
committed
CLOUDSTACK-4813: add function to get ExitValue when run bash commands
1 parent 40716da commit b4397ac

1 file changed

Lines changed: 25 additions & 3 deletions

File tree

‎utils/src/com/cloud/utils/script/Script.java‎

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,8 @@ public String execute(OutputInterpreter interpreter) {
207207
if (interpreter != null) {
208208
return interpreter.drain() ? task.getResult() : interpreter.interpret(ir);
209209
} else {
210-
// null return is ok apparently
211-
return (_process.exitValue() == 0) ? "Ok" : "Failed, exit code " + _process.exitValue();
210+
// null return exitValue apparently
211+
return String.valueOf(_process.exitValue());
212212
}
213213
} else {
214214
break;
@@ -251,7 +251,7 @@ public String execute(OutputInterpreter interpreter) {
251251
error = interpreter.processError(reader);
252252
}
253253
else {
254-
error = "Non zero exit code : " + _process.exitValue();
254+
error = String.valueOf(_process.exitValue());
255255
}
256256

257257
if (_logger.isDebugEnabled()) {
@@ -488,4 +488,26 @@ public static String runSimpleBashScript(String command, int timeout) {
488488
return result.trim();
489489
}
490490

491+
public static int runSimpleBashScriptForExitValue(String command) {
492+
return runSimpleBashScriptForExitValue(command, 0);
493+
}
494+
495+
public static int runSimpleBashScriptForExitValue(String command, int timeout) {
496+
497+
Script s = new Script("/bin/bash", timeout);
498+
s.add("-c");
499+
s.add(command);
500+
501+
String result = s.execute(null);
502+
if (result == null || result.trim().isEmpty())
503+
return -1;
504+
else {
505+
try {
506+
return Integer.valueOf(result.trim());
507+
} catch (NumberFormatException e) {
508+
return -1;
509+
}
510+
}
511+
}
512+
491513
}

0 commit comments

Comments
 (0)