roy.me
roy.me

Reputation: 471

How do I know when a SSH-shell program has finished or parse the log in real time?

I'm writing a GUI in c#, in which when the uesr click on a button, the GUI calls for kitty.exe (which is exactly like PuTTy.exe, with extra features) to run some command in Linux, during this time there's a log on kitty ssh command shell which is passed to a log file (using KiTTY embedded feature). When it is finished, I check the log file for the word 'Succeed' or 'Fail'.

What I do nowadays is I wait (using the 'sleep' command in c#) 2 minutes, then I check the log file. This does work, but I want it to be more efficient, because it sometimes take 90 sec and sometimes 60 sec, and I don't want to wait so long after it is finished. Furthermore, it's not so elegant solution :o). My question is how (and if) can I check the log running on the SSH windows in real time, and search for the key words (succeed or fail), rather than just wait for it to finish and then check it. using the '>' in cmd.exe to call the kiTTy.exe and pass its log to a file does not work, by the way. Thank you!

Upvotes: 0

Views: 310

Answers (1)

T3am5hark
T3am5hark

Reputation: 866

If you invoke your system process using System.Diagnostics.Process.Start(), then you can use the WaitForExit method after invoking. That will block further execution on the thread until the process completes at which point you can check the log. This may be preferable to waiting an arbitrary amount of time.

If you want to do it in real time rather than waiting until completion, there is a facility to get a stream reader via System.Diagnostics.Process.StandardOutput which you can use to look for the item of interest.

Upvotes: 2

Related Questions