trapping TERM in a daemonized (Daemon gem) script

D

Dean Holdren

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?
 
T

Tim Pease

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?

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
 
D

Dean Holdren

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
 

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

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,281
Latest member
Pedroaciny

Latest Threads

Top