8

I'm using System.Diagnostics.Process.Start() to remotely launch commands on a Linux os. Until now I have been able to launch simple commands and then read the output.
For example I can execute the command echo Hello World and read Hello World as its output.

Here's the simplified code:

public void Execute(string file, string args) {
    Process process = new Process {
        StartInfo = {
            FileName = file,
            Arguments = args,
            RedirectStandardOutput = true,
            RedirectStandardError = true,
            UseShellExecute = false
        }
    };
    process.Start();
}

To be clearer, I use the code above like this: Execute("echo", "Hello World");.

Here's my problem: as long as I execute simple commands everything works smooth, but I'd like to launch commands with pipes and redirects, in order to have a stronger control on the command and on its output (without handle the output itself as text).
So, is there a workaround (or maybe a specific library) to achieve this result?

4
  • 3
    I think you have simplified your code just a smidgen too much. There's nothing here indicating how the command is executed on a remote host.
    – Sam Axe
    Commented Jul 1, 2015 at 8:55
  • 1
    Well Pipes are essentially data streams and if you want to have control over the I/O of your target process you won't be able to dodge streams, especially, if you are working cross-platform, as Windows implements streams/pipes differently than Linux and .NET provides an abstract layer by giving you the Stream class.
    – MrPaulch
    Commented Jul 1, 2015 at 8:58
  • 1
    You'll need to get the standard pipes from the process for example Process.StandardOutput - msdn.microsoft.com/en-us/library/…
    – Lloyd
    Commented Jul 1, 2015 at 9:08
  • So, in case I want to use pipes, I basically have to read the Process.StandardOutput and then handle what I read, right?
    – xnr_z
    Commented Jul 1, 2015 at 9:31

1 Answer 1

11

In order to execute commands in Linux with all the shell features (including pipelines, redirections etc.) use the following code:

public static Process ExecuteInBash(string command)
{
    var process = new Process
    {
        StartInfo = 
        {
            FileName = "bash",
            ArgumentList = { "-c", "--", command },
            RedirectStandardOutput = true,
            RedirectStandardError = true,
            UseShellExecute = false
        }
    };
    process.Start();
    return process;
}

Mind that this method is non-blocking, it starts the process and does not wait for it to finish. In order to block you have to call the process.WaitForExit() method. Also, it is a good idea to check the exit code to find out whether the command succeeded or failed. Example:


var process = ExecuteInBash("echo Hello");
process.WaitForExit();
var standardOutput = process.StandardOutput.ReadToEnd();
var standardError = process.StandardError.ReadToEnd();
if (process.ExitCode == 0)
{
    // Command succeeded.
}
2
  • This just does the job as expected. I had tried using sudo as filename, that was failing to do with pipes. bash is excecuting pipe redirection without any issue. Thanks @Dzienny . Commented Sep 30, 2023 at 10:43
  • 1
    @HarishPatil Good that you brought my attention to this old answer, because I noticed an issue with it. In the old code you could not use double quotes within the command without extra escaping. It is fixed now. And I added some remarks.
    – Dzienny
    Commented Oct 14, 2023 at 20:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.