Thread popen and broken pipe problem

L

L.

Hello there, I try to execute a list of command in a thread thanks to
popen.
But during command execution user may wants to interrupt / kill the
execution process.
My code looks like that :

@thread = Thread.new do
@command_list.each {|name, cmd|
IO.popen(cmd){ |@pipe|
pipe.each_line {|l|
puts l
}
}
}
end

But when i try to @thread.kill I have a broken pipe error message on
the current command execution.

How to correctly finish and kill the thread without having such
errors?

thanks.
 
L

L.

Found it,
when io is set after popen use, it's easy to get the process ID and
then to correctly kill it during its execution.


#get the process id

IO.popen(cmd){ |pipe|
current_process = pipe.id # Saving the PID
pipe.each_line {|l|
puts l
}

#kill the process

Process.kill("KILL", current_process)

et voila.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top