# Trapping TERM in a daemonized (Daemon gem) script

**URL:** <https://www.ruby-forum.com/t/trapping-term-in-a-daemonized-daemon-gem-script/115834>\
**Category:** Ruby\
**Created:** [September 28, 2007, 10:57pm UTC](https://www.ruby-forum.com/t/trapping-term-in-a-daemonized-daemon-gem-script/115834 "2007-09-28T22:57:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![deanhiller](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/df705f/32.png) [@deanhiller](https://www.ruby-forum.com/u/deanhiller)\
**Post date:** [September 28, 2007, 10:57pm UTC](https://www.ruby-forum.com/t/trapping-term-in-a-daemonized-daemon-gem-script/115834/1 "2007-09-28T22:57:09Z")

</div>

I’m using the Daemon gem, and I’d like to trap the TERM signal so that  
I can have graceful termination of my main execution loop:

$running = true;  
Signal.trap(“TERM”) do  
$running = false  
end

while($running) do  
#some operations that could take a few seconds  
sleep(5)  
end

However, I get some memory errors when I stop the process, and I can  
sometimes get an extra process after a ‘restart’ is issued, that I  
can’t kill with a ‘stop’

I looked at the source to the Daemon gem, and it warns not to trap the  
TERM signal in your script. So how can I gracefully end my loop if I  
don’t trap TERM?

---

<div class="post-metadata">

**Author:** ![deanhiller](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/df705f/32.png) [@deanhiller](https://www.ruby-forum.com/u/deanhiller)\
**Post date:** [September 29, 2007, 4:44pm UTC](https://www.ruby-forum.com/t/trapping-term-in-a-daemonized-daemon-gem-script/115834/2 "2007-09-29T16:44:43Z")

</div>

On 9/28/07, Dean H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> sleep(5)  
> end
> 
> However, I get some memory errors when I stop the process, and I can  
> sometimes get an extra process after a ‘restart’ is issued, that I  
> can’t kill with a ‘stop’
> 
> I looked at the source to the Daemon gem, and it warns not to trap the  
> TERM signal in your script. So how can I gracefully end my loop if I  
> don’t trap TERM?

You can trap any signal and use that to terminate your script. Try  
the interrupt signal.

Signal.trap(‘INT’) do  
end

To send this to your daemon process on a \*NIX host …

kill -SIGINT pid

Blessings,  
TwP

---

<div class="post-metadata">

**Author:** ![deanhiller](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/df705f/32.png) [@deanhiller](https://www.ruby-forum.com/u/deanhiller)\
**Post date:** [October 1, 2007, 4:16pm UTC](https://www.ruby-forum.com/t/trapping-term-in-a-daemonized-daemon-gem-script/115834/3 "2007-10-01T16:16:46Z")

</div>

well I’d want the ‘daemonized’ script stopped when I issue  
mydaemon\_ctl stop, I think under the hood, daemons gem issues a TERM  
to the controlled script.

not sure if you’re familiar with the daemons gem, but you typically  
create a \_ctl file that controls a ruby script (with an infinite  
loop):

mydaemon\_ctl:  
Daemons.run File.dirname( **FILE** ) + ‘/orderprocessor.rb’, options

mydaemon.rb:  
while(true) do  
end
