# Why don't threads get killed in this case?

**URL:** <https://www.ruby-forum.com/t/why-dont-threads-get-killed-in-this-case/114121>\
**Category:** Ruby\
**Created:** [September 8, 2007, 4:31am UTC](https://www.ruby-forum.com/t/why-dont-threads-get-killed-in-this-case/114121 "2007-09-08T04:31:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![overworked\_phd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/b5a626/32.png) [@overworked\_phd](https://www.ruby-forum.com/u/overworked_phd)\
**Post date:** [September 8, 2007, 4:31am UTC](https://www.ruby-forum.com/t/why-dont-threads-get-killed-in-this-case/114121/1 "2007-09-08T04:31:18Z")

</div>

Here is the code that is running on OpenBSD 4.2

#!/usr/local/bin/ruby

threads=[]

4.times do |i|  
threads[i]=Thread.new do  
%x{/usr/libexec/getty std.19200 tty00}  
end  
end

threads.each{|thr| thr.join}

I run it as a unix background job, I get the following

-bash-3.2$ ./whore.rb &  
[1] 11801  
-bash-3.2$ ps waux | grep djdoboy  
djdoboy 15158 0.0 0.3 844 1540 pi Ss 9:41PM 0:00.05 -  
bash  
(bash)  
djdoboy 11801 0.0 0.4 996 1872 pi S 10:03PM 0:00.01  
/usr/local/bin/ruby ./whore.rb  
djdoboy 4493 0.0 0.1 372 556 pi S 10:03PM 0:00.02  
/usr/libexec/getty std.19200 tty00  
djdoboy 19020 0.0 0.1 332 556 pi S 10:03PM 0:00.01  
/usr/libexec/getty std.19200 tty00  
djdoboy 456 0.0 0.1 364 564 pi S 10:03PM 0:00.02  
/usr/libexec/getty std.19200 tty00  
djdoboy 11853 0.0 0.1 260 584 pi S 10:03PM 0:00.01  
/usr/libexec/getty std.19200 tty00  
djdoboy 23871 0.0 0.1 532 408 pi R+ 10:03PM 0:00.00 ps  
-waux  
djdoboy 27426 0.0 0.1 220 524 pi S+ 10:03PM 0:00.00 grep  
djdoboy

Now I get and relogin  
-bash-3.2$ exit  
logout  
Connection closed by foreign host.  
[cdalten@localhost ~]$ telnet

-bash-3.2$ ps waux | grep djdoboy  
djdoboy 6724 0.0 0.3 1012 1548 p6 Ss 10:04PM 0:00.02 -  
bash  
(bash)  
djdoboy 25303 0.0 0.1 576 448 p6 R+ 10:04PM 0:00.00 ps  
-waux  
djdoboy 8516 0.0 0.1 1012 408 p6 R+ 10:04PM 0:00.00 grep  
djdoboy (bash)  
djdoboy 11801 0.0 0.4 996 1872 pi- S 10:03PM 0:00.01  
/usr/local/bin/ruby ./whore.rb  
djdoboy 11853 0.0 0.1 260 584 pi- I 10:03PM 0:00.01  
/usr/libexec/getty std.19200 tty00  
djdoboy 456 0.0 0.1 364 564 pi- I 10:03PM 0:00.02  
/usr/libexec/getty std.19200 tty00  
djdoboy 4493 0.0 0.1 372 556 pi- I 10:03PM 0:00.02  
/usr/libexec/getty std.19200 tty00  
djdoboy 19020 0.0 0.1 332 556 pi- I 10:03PM 0:00.01  
/usr/libexec/getty std.19200 tty00

And the threads are still there. How come ruby threads don’t get  
killed  
on exit? And more to the point, how do I kill them on exit?

Chad

---

<div class="post-metadata">

**Author:** ![overworked\_phd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/b5a626/32.png) [@overworked\_phd](https://www.ruby-forum.com/u/overworked_phd)\
**Post date:** [September 8, 2007, 9:49am UTC](https://www.ruby-forum.com/t/why-dont-threads-get-killed-in-this-case/114121/2 "2007-09-08T09:49:33Z")

</div>

Hi Chad,

On Sat, Sep 08, 2007 at 11:30:04AM +0900, grocery\_stocker wrote:

> end  
> (bash)  
> djdoboy 23871 0.0 0.1 532 408 pi R+ 10:03PM 0:00.00 ps  
> -bash-3.2$ ps waux | grep djdoboy  
> /usr/libexec/getty std.19200 tty00

This is not a ruby isssue, this is related to your shell options.

% uname -rs  
OpenBSD 4.0  
% find ./ -type f \> /dev/null &  
[1] 6962  
% exit

# log back in

% ps -p 6962  
PID TT STAT TIME COMMAND  
6962 p4 D 0:00.95 find ./ -type f  
% kill 6962  
% shopt huponexit  
huponexit off

man bash:

```
   If the huponexit shell option has been set with shopt,
   bash sends a SIGHUP to all jobs when an interactive login
   shell exits.

```

So lets try it.

% shopt -s huponexit  
% find ./ -type f \> /dev/null &  
[1] 29709  
% ps -p 29709  
PID TT STAT TIME COMMAND  
29709 p4 D 0:01.65 find ./ -type f  
% exit

# log back in

% ps -p 29709  
PID TT STAT TIME COMMAND  
%

enjoy,

-jeremy

---

<div class="post-metadata">

**Author:** ![overworked\_phd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/b5a626/32.png) [@overworked\_phd](https://www.ruby-forum.com/u/overworked_phd)\
**Post date:** [September 8, 2007, 11:17am UTC](https://www.ruby-forum.com/t/why-dont-threads-get-killed-in-this-case/114121/3 "2007-09-08T11:17:38Z")

</div>

Solve it in “ensure” clausure ;)…

Yours,  
Killavus.
