Help diagnosing why my program isn't trapping SIGTERM on shutdown

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I have a program which runs in the background, doing its thing.  When it
receives a SIGTERM it will tidy up and then exit.  In test - running it
from a TTY, either in foreground or background, it behaves as expected.
  But when I restart Linux (Fedora Core 4, using 'shutdown -r now') the
code that traps SIGTERM isn't executed, despite the Linxu manual
specifically saying that SIGTERM is passed to running processes.  Can
someone who has encountered the problem help me to diagnose it?  (I am
aware that it might turn out to be a Linux problem, in which case I'll
take it somewhere else ...!)

Here's the core of the code, called

#! /usr/bin/perl
use strict;
use warnings;
use Sys::Syslog;
use sigtrap 'handler' => \&terminate, 'normal-signals';
my $loop_count = 0;
my $timer = 0;
my $sleep_size = 10;
my $loop_size = 6;
openlog '', 'cons,pid', 'user';
syslog 'info', 'starting up';
while (1) {
    while (++$loop_count < $loop_size) {
        sleep $sleep_size;
    $timer += $loop_count*$sleep_size;
    $loop_count = 0;
    syslog 'info', "Running for $timer seconds";
syslog 'info', 'How did we get here?';

sub terminate {
    syslog 'info', 'Normal signal received';
    exit 0;

Here's the appropriate bit of the system log when I send signals manually:

    Here's where CRON starts the task
Sep  1 16:43:01 neptune crond(pam_unix)[2263]: session opened for user
nfb by (uid=0)
Sep  1 16:43:02 neptune[2264]: starting up
    Then I do "kill -TERM 2264" from a TTY and ...
Sep  1 16:43:29 neptune[2264]: Normal signal received
Sep  1 16:43:29 neptune crond(pam_unix)[2263]: session closed for user nfb
    ... that's where it handles the signal and stops.

But here's the system log when I reboot the machine with the "shutdown"

Sep  1 16:45:02 neptune crond(pam_unix)[2283]: session opened for user
nfb by (uid=0)
Sep  1 16:45:02 neptune[2284]: starting up
    ... lines deleted
Sep  1 16:45:52 neptune[2284]: Running for 60 seconds
    It was definitely running ...
Sep  1 16:46:15 neptune shutdown: shutting down for system reboot
Sep  1 16:46:15 neptune init: Switching to runlevel: 6
Sep  1 16:46:17 neptune xfs[1575]: terminating
Sep  1 16:46:19 neptune nmbd[1595]: [2006/09/01 16:46:19, 0]
Sep  1 16:46:19 neptune nmbd[1595]:   Got SIGTERM: going down...
    ... etc until the reboot happens. just vanishes!

Perl version is 5.8.6 running on Intel.


Henry Law       <><     Manchester, England

Re: Help diagnosing why my program isn't trapping SIGTERM on shutdown

Henry Law wrote:
Quoted text here. Click to load it

er.  I may be missing something, but it sounds like you've *already*
determined it's a Linux problem, not a Perl problem.  But to check,
write a program in another language, say C for example.  Trap SIGTERM
there.  Call it in *exactly* the same manner that you call your current
Perl program.  Does its signal handling code receive a SIGTERM on Linux

Paul Lalli

Site Timeline