Frage

I have a bit of code that runs a command-line tool. I want to capture the output of this tool so I can inspect it for error messages, but the command line bit can also hang, so I need to kill it if it takes too long.

I don't need streaming access to the output of the command (it's not very verbose), only the result.

Is there a good way to do this?

I can get the output by calling the tool with back-tics, and I can wrap that in a Thread or Process to kill if it takes too long, but then I'm not sure how to get the results out.

War es hilfreich?

Lösung

Oh, duh - Thread.value evaluates to the output of the last expression in the thread, and join takes a time limit. So it's as simple as:

thread = `Thread.new{ `command options` }.join(180)
results = thread.value unless thread.nil?

Need to do the nil check since if you kill the thread you get nil out.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top