updated script for shell cmd logging

This commit is contained in:
Nathan Freitas 2012-01-16 22:54:42 -05:00
parent ee9e3d7c8b
commit 9935cc8784
1 changed files with 40 additions and 46 deletions

View File

@ -161,57 +161,51 @@ public class TorServiceUtils implements TorServiceConstants {
public static int doShellCommand(String[] cmds, StringBuilder log, boolean runAsRoot, boolean waitFor) throws Exception public static int doShellCommand(String[] cmds, StringBuilder log, boolean runAsRoot, boolean waitFor) throws Exception
{ {
TorService.logMessage("executing shell cmds: " + cmds[0] + "; runAsRoot=" + runAsRoot); TorService.logMessage("executing shell cmds: " + cmds[0] + "; runAsRoot=" + runAsRoot + ";waitFor=" + waitFor);
Process proc = null; Process proc = null;
int exitCode = -1; int exitCode = -1;
if (runAsRoot)
if (runAsRoot) proc = Runtime.getRuntime().exec("su");
proc = Runtime.getRuntime().exec("su"); else
else proc = Runtime.getRuntime().exec("sh");
proc = Runtime.getRuntime().exec("sh");
OutputStreamWriter out = new OutputStreamWriter(proc.getOutputStream());
OutputStreamWriter out = new OutputStreamWriter(proc.getOutputStream());
for (int i = 0; i < cmds.length; i++)
for (int i = 0; i < cmds.length; i++) {
{ out.write(cmds[i]);
out.write(cmds[i]); out.write("\n");
out.write("\n"); }
}
out.flush();
out.flush(); out.write("exit\n");
out.write("exit\n"); out.flush();
out.flush();
if (waitFor)
if (waitFor) {
{
final char buf[] = new char[10];
final char buf[] = new char[10];
// Consume the "stdout"
// Consume the "stdout" InputStreamReader reader = new InputStreamReader(proc.getInputStream());
InputStreamReader reader = new InputStreamReader(proc.getInputStream()); int read=0;
int read=0; while ((read=reader.read(buf)) != -1) {
while ((read=reader.read(buf)) != -1) { if (log != null) log.append(buf, 0, read);
if (log != null) log.append(buf, 0, read);
}
// Consume the "stderr"
reader = new InputStreamReader(proc.getErrorStream());
read=0;
while ((read=reader.read(buf)) != -1) {
if (log != null) log.append(buf, 0, read);
}
exitCode = proc.waitFor();
log.append("process exit code: ");
log.append(exitCode);
log.append("\n");
TorService.logMessage("command process exit value: " + exitCode);
} }
// Consume the "stderr"
reader = new InputStreamReader(proc.getErrorStream());
read=0;
while ((read=reader.read(buf)) != -1) {
if (log != null) log.append(buf, 0, read);
}
exitCode = proc.waitFor();
}
return exitCode; return exitCode;