# Thread popen and broken pipe problem

**URL:** <https://www.ruby-forum.com/t/thread-popen-and-broken-pipe-problem/148593>\
**Category:** Ruby\
**Created:** [September 15, 2008, 1:00pm UTC](https://www.ruby-forum.com/t/thread-popen-and-broken-pipe-problem/148593 "2008-09-15T13:00:51Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![LLL](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/4491bb/32.png) [@LLL](https://www.ruby-forum.com/u/LLL)\
**Post date:** [September 15, 2008, 1:00pm UTC](https://www.ruby-forum.com/t/thread-popen-and-broken-pipe-problem/148593/1 "2008-09-15T13:00:51Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![LLL](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/4491bb/32.png) [@LLL](https://www.ruby-forum.com/u/LLL)\
**Post date:** [September 15, 2008, 11:42pm UTC](https://www.ruby-forum.com/t/thread-popen-and-broken-pipe-problem/148593/2 "2008-09-15T23:42:21Z")

</div>

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.
