Skip to content

Instantly share code, notes, and snippets.

@lincolnthomas
Created January 17, 2017 19:58
Show Gist options
  • Save lincolnthomas/90ae5a19c1ced1094ec035650f1f061b to your computer and use it in GitHub Desktop.
Save lincolnthomas/90ae5a19c1ced1094ec035650f1f061b to your computer and use it in GitHub Desktop.
Show return code, stdout, and stderr from shell commands
// Add the "LPT" section to this method (or similarly for other the other run...() methods in this class).
// Existing code as of 17-Jan-2017 from eucalyptus/clc/modules/core/src/main/java/edu/ucsb/eucalyptus/util/SystemUtil.java
//...
public static int runAndGetCode(String[] command) {
boolean hasTicket = maybeRestrictedOp( command );
try
{
String commandString = "";
for(String part : command) {
commandString += part + " ";
}
LOG.debug("Running command: " + commandString);
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(command);
StreamConsumer error = new StreamConsumer(proc.getErrorStream());
StreamConsumer output = new StreamConsumer(proc.getInputStream());
error.start();
output.start();
int returnValue = proc.waitFor();
//LPT added for debug
error.join();
output.join();
LOG.debug("Command return code = " + returnValue + "\n output = '" + output.getReturnValue() +
"'\n error = '" + error.getReturnValue() + "'");
//LPT end of added debug code
return returnValue;
} catch (Exception t) {
LOG.error(t, t);
} finally {
if ( hasTicket ) releaseRestrictedOp( command );
}
return -1;
}
//...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment