private int RunNativeCommand(string command, string parameters, string workingDirectory, bool wait, bool consumeStreams) { string[] cmd; string[] args = new string[2]; args[0] = command; args[1] = parameters; int result = -1; try { //with this variable will be done the switching string osName = Environment.GetEnvironmentVariable("OS"); if (osName == null) { throw new JobExecutionException("Could not read environment variable for OS"); } if (osName.ToLower().IndexOf("windows") > -1) { cmd = new string[args.Length + 2]; cmd[0] = "cmd.exe"; cmd[1] = "/C"; for (int i = 0; i < args.Length; i++) { cmd[i + 2] = args[i]; } } else if (osName.ToLower().IndexOf("linux") > -1) { cmd = new String[3]; cmd[0] = "/bin/sh"; cmd[1] = "-c"; cmd[2] = args[0] + " " + args[1]; } else { // try this... cmd = args; } // Executes the command string temp = ""; for (int i = 1; i < cmd.Length; i++) { temp += cmd[i] + " "; } temp = temp.Trim(); Log.Info(string.Format(CultureInfo.InvariantCulture, "About to run {0} {1}...", cmd[0], temp)); Process proc = new Process(); proc.StartInfo.FileName = cmd[0]; proc.StartInfo.Arguments = temp; proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; proc.StartInfo.CreateNoWindow = true; proc.StartInfo.UseShellExecute = false; proc.StartInfo.RedirectStandardError = true; proc.StartInfo.RedirectStandardOutput = true; if (!String.IsNullOrEmpty(workingDirectory)) { proc.StartInfo.WorkingDirectory = workingDirectory; } proc.Start(); // Consumes the stdout from the process StreamConsumer stdoutConsumer = new StreamConsumer(this, proc.StandardOutput.BaseStream, StreamTypeStandardOutput); // Consumes the stderr from the process if (consumeStreams) { StreamConsumer stderrConsumer = new StreamConsumer(this, proc.StandardError.BaseStream, StreamTypeError); stdoutConsumer.Start(); stderrConsumer.Start(); } if (wait) { proc.WaitForExit(); result = proc.ExitCode; } // any error message? } catch (Exception x) { throw new JobExecutionException("Error launching native command: " + x.Message, x, false); } return result; }
private int RunNativeCommand(string command, string parameters, string workingDirectory, bool wait, bool consumeStreams) { string[] cmd; string[] args = new string[2]; args[0] = command; args[1] = parameters; int result = -1; try { //with this variable will be done the switching string osName = Environment.GetEnvironmentVariable("OS"); if (osName == null) { throw new JobExecutionException("Could not read environment variable for OS"); } if (osName.ToLower().IndexOf("windows") > -1) { cmd = new string[args.Length + 2]; cmd[0] = "cmd.exe"; cmd[1] = "/C"; for (int i = 0; i < args.Length; i++) { cmd[i + 2] = args[i]; } } else if (osName.ToLower().IndexOf("linux") > -1) { cmd = new string[3]; cmd[0] = "/bin/sh"; cmd[1] = "-c"; cmd[2] = args[0] + " " + args[1]; } else { // try this... cmd = args; } // Executes the command string temp = ""; for (int i = 1; i < cmd.Length; i++) { temp += cmd[i] + " "; } temp = temp.Trim(); Log.Info($"About to run {cmd[0]} {temp}..."); Process proc = new Process(); proc.StartInfo.FileName = cmd[0]; proc.StartInfo.Arguments = temp; #if WINDOWS_PROCESS proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; #endif // WINDOWS_PROCESS proc.StartInfo.CreateNoWindow = true; proc.StartInfo.UseShellExecute = false; proc.StartInfo.RedirectStandardError = true; proc.StartInfo.RedirectStandardOutput = true; if (!string.IsNullOrEmpty(workingDirectory)) { proc.StartInfo.WorkingDirectory = workingDirectory; } proc.Start(); // Consumes the stdout from the process StreamConsumer stdoutConsumer = new StreamConsumer(this, proc.StandardOutput.BaseStream, StreamTypeStandardOutput); // Consumes the stderr from the process if (consumeStreams) { StreamConsumer stderrConsumer = new StreamConsumer(this, proc.StandardError.BaseStream, StreamTypeError); stdoutConsumer.Start(); stderrConsumer.Start(); } if (wait) { proc.WaitForExit(); result = proc.ExitCode; } // any error message? } catch (Exception x) { throw new JobExecutionException("Error launching native command: " + x.Message, x, false); } return(result); }
private int RunNativeCommand(String command, string parameters, bool wait, bool consumeStreams) { string[] cmd; var args = new string[2]; args[0] = command; args[1] = parameters; var result = -1; try { //with this variable will be done the swithcing var osName = Environment.GetEnvironmentVariable("OS"); if (osName == null) { throw new JobExecutionException("Could not read environment variable for OS"); } if (osName.ToLower().IndexOf("windows") > -1) { cmd = new string[args.Length + 2]; cmd[0] = "cmd.exe"; cmd[1] = "/C"; for (var i = 0; i < args.Length; i++) { cmd[i + 2] = args[i]; } } else if (osName.ToLower().IndexOf("linux") > -1) { cmd = new String[3]; cmd[0] = "/bin/sh"; cmd[1] = "-c"; cmd[2] = args[0] + " " + args[1]; } else { // try this... cmd = args; } // Executes the command var temp = ""; for (var i = 1; i < cmd.Length; i++) { temp += cmd[i] + " "; } temp = temp.Trim(); Log.Info(string.Format(CultureInfo.InvariantCulture, "About to run {0} {1}...", cmd[0], temp)); var proc = new Process(); proc.StartInfo.FileName = cmd[0]; proc.StartInfo.Arguments = temp; proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; proc.StartInfo.CreateNoWindow = true; proc.StartInfo.UseShellExecute = false; proc.StartInfo.RedirectStandardError = true; proc.StartInfo.RedirectStandardOutput = true; proc.Start(); // Consumes the stdout from the process var stdoutConsumer = new StreamConsumer(this, proc.StandardOutput.BaseStream, "stdout"); // Consumes the stderr from the process if (consumeStreams) { var stderrConsumer = new StreamConsumer(this, proc.StandardError.BaseStream, "stderr"); stdoutConsumer.Start(); stderrConsumer.Start(); } if (wait) { proc.WaitForExit(); result = proc.ExitCode; } // any error message? } catch (Exception x) { throw new JobExecutionException("Error launching native command: " + x.Message, x, false); } return(result); }