# How to log and chain signals

**URL:** <https://www.ruby-forum.com/t/how-to-log-and-chain-signals/152603>\
**Category:** Ruby\
**Created:** [November 5, 2008, 2:25pm UTC](https://www.ruby-forum.com/t/how-to-log-and-chain-signals/152603 "2008-11-05T14:25:12Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![frocco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/a87d85/32.png) [@frocco](https://www.ruby-forum.com/u/frocco)\
**Post date:** [November 5, 2008, 2:25pm UTC](https://www.ruby-forum.com/t/how-to-log-and-chain-signals/152603/1 "2008-11-05T14:25:12Z")

</div>

Hi everyone,

I’ve been running my own ruby dispatcher on my website for a few years  
now, but I keep having problems with FCGI and no amount of logging has  
ever come up with any answers…

The problem is that the processes spawned and managed by apache’s fcgi  
server (Fast CGI) sometimes quit for no apparent reason or keep running  
even after they have been asked to shutdown.

I’ve been totally flummoxed by all this until I read today that FCGI  
installs its own Signal Handlers: I had been living in a dream world  
where a thread has a single path of execution and simply couldn’t figure  
out where things went wrong (I’m more of a Java man than a Unix guy).

I’m fairly certain now that it’s something to do with FCGI responding  
incorrectly to signals and I want to log the signal without blocking  
their handling.

My aim would be to log the signal being received by the process and then  
execute whatever would originally have happened.

My naive attempt at doing this for SIGTERM is below:

PREV\_SIGNAL\_HANDLERS = Hash.new

PREV\_SIGNAL\_HANDLERS[‘TERM’] = trap(“TERM”) do  
puts “(pid: #{Process.pid}) TERM signal received.”;  
puts “previous handler: #{PREV\_SIGNAL\_HANDLERS[‘TERM’]} of type:  
#{PREV\_SIGNAL\_HANDLERS[‘TERM’].class}”

if PREV\_SIGNAL\_HANDLERS[‘TERM’].class == Proc.class then  
$stderr.puts “(pid: #{Process.pid}) calling previous call chain”  
PREV\_SIGNAL\_HANDLERS[‘TERM’].call  
else  
$stderr.puts “(pid: #{Process.pid}) calling handler:  
#{PREV\_SIGNAL\_HANDLERS[‘TERM’]}”  
trap(‘TERM’, PREV\_SIGNAL\_HANDLERS[‘TERM’])  
Process.kill(‘TERM’, Process.pid)  
end  
end

This works fine in a sample script, but doesn’t seem to be working at  
all when I add it to my dispatcher…

Any advice would be appreciated.

Best regards,

Frank
