You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Cannot get streaming stdout/stderr and wait for remote process to exit #164
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.
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.
You could also execute the command with extra " 2>&1" so the stderr is also routed to stdout and is readable in the cmd.Result.
You will only get the result when it's finished, but still better than no stderr. using (var cmd = sshclient.CreateCommand(command + " 2>&1")) { if(timeout > 0) cmd.CommandTimeout = TimeSpan.FromSeconds(timeout); cmd.Execute(); result = cmd.Result; retval = cmd.ExitStatus; //Although the command is within a using statement, this //dispose resolves some internal SSH problems when connecting //to non-existing controllers. cmd.Dispose(); }
Activity
drieseng commentedon Feb 8, 2017
We've started working on this as part of PR #144, but I got sidetracked by some other issues. I'll soon reboot my work on this.
alanmcgovern commentedon Feb 8, 2017
Oh perfect! I'll keep an eye on that PR. If you want to close this immediately feel free, otherwise we can close this when PR #144 is ready :)
mvdhorst commentedon Nov 10, 2020
You could also execute the command with extra " 2>&1" so the stderr is also routed to stdout and is readable in the cmd.Result.
You will only get the result when it's finished, but still better than no stderr.
using (var cmd = sshclient.CreateCommand(command + " 2>&1")) { if(timeout > 0) cmd.CommandTimeout = TimeSpan.FromSeconds(timeout); cmd.Execute(); result = cmd.Result; retval = cmd.ExitStatus; //Although the command is within a using statement, this //dispose resolves some internal SSH problems when connecting //to non-existing controllers. cmd.Dispose(); }