private string RunCommandsAsBatch(List <CommandExecutionParamModel> commandLineModels, int processTimeoutMs) { var fileModel = GetExecuteBatchFileModel(); var output = string.Empty; try { //To create a batch file using (var sw = new StreamWriter(fileModel.FileName)) { if (fileModel.IsUnixOS) { sw.WriteLine("#!/bin/bash"); } foreach (var model in commandLineModels) { sw.WriteLine($"{model.Command} {model.Arguments}"); } } //Reset the command with batch file name. var newCommand = new CommandExecutionParamModel(); newCommand.Command = fileModel.FileName; //To change the file permission on unix box if (fileModel.IsUnixOS) { var command = "chmod 755 " + fileModel.FileName; ExecuteUnixCommand(command); } output = RunSingleProcess(newCommand, processTimeoutMs); } catch (Exception ex) { throw; } finally { //Console.WriteLine("filename" + fileModel.FileName); File.Delete(fileModel.FileName); } return(output); }
private List <CommandExecutionParamModel> ParseCommands(string[] commands) { var commandLineModels = new List <CommandExecutionParamModel>(); for (int i = 0; i < commands.Length; i++) { var model = new CommandExecutionParamModel(); if (commands[i].IndexOf(" ") > 0) { var index = commands[i].IndexOf(' '); model.Command = commands[i].Substring(0, index); model.Arguments = commands[i].Substring(index); } else { model.Command = commands[i]; } commandLineModels.Add(model); } return(commandLineModels); }
private string RunSingleProcess(CommandExecutionParamModel model, int processTimeoutMs) { var process = new System.Diagnostics.Process(); var output = string.Empty; try { var outputBuilder = new StringBuilder(); ProcessStartInfo processStartInfo = new ProcessStartInfo { CreateNoWindow = true, RedirectStandardOutput = true, RedirectStandardInput = true, RedirectStandardError = true, UseShellExecute = false, ErrorDialog = false, FileName = model.Command }; process.StartInfo = processStartInfo; // enable raising events because Process does not raise events by default process.EnableRaisingEvents = true; // attach the event handler for OutputDataReceived before starting the process process.OutputDataReceived += (sender, eventArgs) => outputBuilder.AppendLine(eventArgs.Data); process.ErrorDataReceived += (sender, eventArgs) => outputBuilder.AppendLine(eventArgs.Data); // start the process // then begin asynchronously reading the output // then wait for the process to exit // then cancel asynchronously reading the output process.Start(); process.BeginOutputReadLine(); process.BeginErrorReadLine(); if (processTimeoutMs > 0) { var processExited = process.WaitForExit(processTimeoutMs); if (!processExited) { process.Kill(); throw new TimeoutException($"Process did not finish in {processTimeoutMs} ms."); } } else { process.WaitForExit(); } process.CancelOutputRead(); output = outputBuilder.ToString(); return(output); } catch (Exception e) { // Console.WriteLine(e.Message); throw; } finally { process.Close(); } }