Skip to content

Commit 3a999e7

Browse files
committed
CLOUDSTACK-4813: add function to get ExitValue when run bash commands
(cherry picked from commit b4397ac)
1 parent 5ed493c commit 3a999e7

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
@@ -201,8 +201,8 @@ public String execute(OutputInterpreter interpreter) {
201201
if (interpreter != null) {
202202
return interpreter.drain() ? task.getResult() : interpreter.interpret(ir);
203203
} else {
204-
// null return is ok apparently
205-
return (_process.exitValue() == 0) ? "Ok" : "Failed, exit code " + _process.exitValue();
204+
// null return exitValue apparently
205+
return String.valueOf(_process.exitValue());
206206
}
207207
} else {
208208
break;
@@ -245,7 +245,7 @@ public String execute(OutputInterpreter interpreter) {
245245
error = interpreter.processError(reader);
246246
}
247247
else {
248-
error = "Non zero exit code : " + _process.exitValue();
248+
error = String.valueOf(_process.exitValue());
249249
}
250250

251251
if (_logger.isDebugEnabled()) {
@@ -482,4 +482,26 @@ public static String runSimpleBashScript(String command, int timeout) {
482482
return result.trim();
483483
}
484484

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

0 commit comments

Comments
 (0)