|P is for Practical|
How to run a command, gather its output and kill it after a specified time?by blindluke (Hermit)
|on Feb 15, 2012 at 13:49 UTC||Need Help??|
blindluke has asked for the
wisdom of the Perl Monks concerning the following question:
I'm trying to run a command that runs some kind of interactive shell, executes a script, and then stays in the shell. It's stubborn, and does not exit the shell, even if I explicitly provide an exit command in the script file. Also, it hangs frequently, so I cannot be sure that for a given script, I will receive any output (so I cannot use a solution that would open the command with a pipe, then read first X lines, then kill it).
Right now, I'm trying to adopt the idea found here: Re: killing command if it takes too much time. I want all the lines, so I modified it a bit. This is the code after my modifications, and the way I use this sub later:
This, however, does not work. It just times out with "Eval failed because alarm timed out". How can I gather the output, and then kill the program, after a given time (let's say, 20 seconds)?
I could always do it with Expect (especially since I can exit the shell without problems when in interactive mode), but maybe there is an easier solution. If Expect is the right way to do this, please, let me know.