5 Replies - 9268 Views - Last Post: 16 January 2013 - 01:47 AM

#1 apaddobs  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 49
  • Joined: 16-May 12

Trap "kill" signals in Perl.

Posted 15 January 2013 - 09:15 AM

 use sigtrap qw (handler signal_handler normal-signals);



If I run a process that has a sleep (1000), and kill the program using "kill pid", the signal is trapped by virtue of the line above. However, if I kill it using "kill -9 pid", the signal is not trapped.

How do i trap that?
Is This A Good Question/Topic? 0
  • +

Replies To: Trap "kill" signals in Perl.

#2 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7877
  • View blog
  • Posts: 13,376
  • Joined: 19-March 11

Re: Trap "kill" signals in Perl.

Posted 15 January 2013 - 09:26 AM

I believe that what -9 means is precisely that it can't be trapped. That's always how I've understood the situation, at least.
It's a last-resort signal: it'll always work, but it's always messy.
Was This Post Helpful? 1
  • +
  • -

#3 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2133
  • View blog
  • Posts: 3,267
  • Joined: 21-June 11

Re: Trap "kill" signals in Perl.

Posted 15 January 2013 - 09:38 AM

View Postjon.kiparsky, on 15 January 2013 - 05:26 PM, said:

It's a last-resort signal: it'll always work, but it's always messy.


Not always. There are some cases where a process can't be killed (if, say, a syscall is hanging because of a buggy driver or faulty hardware).

That's only tangentially relevant though - you're right that SIGKILL can't be trapped.
Was This Post Helpful? 1
  • +
  • -

#4 apaddobs  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 49
  • Joined: 16-May 12

Re: Trap "kill" signals in Perl.

Posted 15 January 2013 - 09:43 AM

So if I have a plugin runnin a perl program and the java code fails with FATAL:null, with a SIGKILL , then I cannot trap it. Is that right ?
Was This Post Helpful? 0
  • +
  • -

#5 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7877
  • View blog
  • Posts: 13,376
  • Joined: 19-March 11

Re: Trap "kill" signals in Perl.

Posted 15 January 2013 - 09:44 AM

Correction noted. Would it be better to say, "if anything short of powercycle will work, kill -9 will work"?
Was This Post Helpful? 0
  • +
  • -

#6 dsherohman  Icon User is offline

  • Perl Parson
  • member icon

Reputation: 226
  • View blog
  • Posts: 654
  • Joined: 29-March 09

Re: Trap "kill" signals in Perl.

Posted 16 January 2013 - 01:47 AM

View Postapaddobs, on 15 January 2013 - 05:43 PM, said:

So if I have a plugin runnin a perl program and the java code fails with FATAL:null, with a SIGKILL , then I cannot trap it. Is that right ?


Yes. Your process never actually receives the SIGKILL, so it can't handle or respond to it in any way. While most other signals are requests for the target process to do something, SIGKILL is handled directly by the kernel and tells it to stop giving processing time to the target process and to de-allocate all of the target's resources as soon as it is safe to do so.

View Postjon.kiparsky, on 15 January 2013 - 05:44 PM, said:

Correction noted. Would it be better to say, "if anything short of powercycle will work, kill -9 will work"?


Yes and no...

While a process is waiting on an I/O operation (a process status of 'D' in top or ps), the kernel will basically just ignore it, just in case aborting the I/O would cause problems. If the I/O call never completes, then kill -9 doesn't have any effect on it and nothing short of a power cycle will work. However, it sometimes does happen that a process will recover from an I/O hang, such as if it's trying to read from an NFS-mounted volume when the network is down, then the network comes back. In this case, the kill -9 will have its normal effect once the I/O call is able to complete.

So, yes in that, for as long as kill -9 doesn't work, nothing else will; no in that something else may happen which allows the process to continue, at which point kill -9 will work normally.
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1