In my Ruby script, I call on a Perl script and wait for it to finish executing. However, there are times that the Perl script encounters a series of errors and I would like Ruby to be able to handle these errors automatically. So, I implemented the following…
begin
IO.popen(cmdLineExecution) do |stream|
stream.each do |line|
puts line
if line =~ /Some line that I know is an error/
raise MyOwnException
end
end
end
begin
#Wait on the child process
Process.waitpid
rescue Errno::ECHILD
end
rescue MyOwnException
#Abort the command mid processing, and handle the error
end
However, the Perl script continues to execute even though an exception has been thrown, only that it is not piping the output to STDOUT anymore. At this point, if I want to stop the Perl process, I have to go into the Task Manager and manually stop it. Then Process.waitpid ends and continues from there. It is either that or I stop Ruby and the Perl process continues to run in the background and I still have to manually stop it.
BTW: This is on Windows
So thus the question is how do I stop IO.popen without the Perl process from becoming a orphan process mid process?
So – disclaimer, I am using Ruby 1.8.6 and on Windows. It is the only Ruby that the software I use currently supports, so there may be a more elegant solution. Overall, it came down to making sure the process died using the Process.kill command before continuing execution.
I made both the exceptions standard classes that inherit
Exception.