Trapping TERM in a daemonized (Daemon gem) script

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?

On 9/28/07, Dean H. [email protected] 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

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