Description
My scenario is that I want to start a process on a remote machine and wait for it to either crash, or exit gracefully. While I am waiting for it to crash/exit I want to propagate it's stdout/stderr to the host machine so I can parse it, or redirect it to file once I get the data I need from it.
My attempt is: https://gist.github.com/alanmcgovern/c12867f3accc6cf5870d5212d3425974#file-test-cs-L8
The issue with this approach is that the very first (line = reader.ReadLine ())
returns null if you do it 'too quickly'. PipeStream thinks the stream has completed even though the process is still running on the remote machine. If i put a Thread.Sleep before that line I can read some output before i get a null, but eventually i get a null when reading, even though the process hasn't exited.
The only 'reliable' approach to detect when my process exits is to create a ShellStream, then write this command: path/to/long_lived_application; echo TOMBSTONE;
and then exit my read-loop when i see the string TOMBSTONE
appear in the output.
This approach is here: https://gist.github.com/alanmcgovern/c7bf0fab9760cfcd324573df51c98fbb
Is there a better way to asynchronously run a command and gather it's stdout/stderr?