> -----Original Message-----
> From: Ramprasad A Padmanabhan [mailto:[EMAIL PROTECTED]] 
> Sent: 13 September 2002 10:30
> To: [EMAIL PROTECTED]
> Subject: when does a program get __DIE__
> 

>     Can anyone tell me under what conditions does a program get SIGNAL
> __DIE__ under linux
> 
> if I catch it using $SIG{__DIE__} = \&myfunc();
> Then will I be able to kill to script using sig TERM
> 

I'm not sure if I understand your question - your process will die when
killed, unless you have subverted the normal signal handling. It is
mucho more interesting, to die gracefully - then you might need some
signal handling.

Be warned though, Signal handling can be bad for your mental health -
signals arrive anywhen, and your script can be in anystate [or even
outside the US].

The safest thing to do is NOT to invoke your function from the signal
handler - instead set some global var to a single value. The signal
handler should execute just one instruction. If it changes anything it
should only modify already existing memory storage. 

At safe places in your software you can then detect that a signal was
received and exit, etc.

This is a snippet from our process monitoring daemon:

# Right at the top of your script - before includes et al
# declare and initialise a global variable for signal handling
$G::SIGNAL = 0;

# NEVER do ANYTHING in a SIG except increment a counter for checking
elsewhere
$SIG{HUP}    = 'IGNORE' unless $G::OS eq 'NT'; # run nohup except for NT
$SIG{INT}    = sub{$G::SIGNAL = 1;}; # Ctrl-C
$SIG{TERM}   = sub{$G::SIGNAL = 2;}; # kill

sub checkSIGS {
  return unless $G::SIGNAL;
  die "Killed by Ctrl-C"       if $G::SIGNAL == 1;
  die "Killed by TERM signal"  if $G::SIGNAL == 2;
  die "Whoops?? killed by unknown signal? $G::SIGNAL";
}

<do stuff>

<at various safe times in your code/loop>
  checkSIGS();

Regards
Jeff


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to