Handling KILL signal in a perl script

S

Swarup Baran

Hello All,

Here is what the scenario is...

I have a perl script which runs for hours reading data and processing
it.

What i need to handle is if this perl process gets a kill process
either because of a manual kill or machine reboot etc. I should be
able to trap it, clean up if any resources and then EXIT.

Is it possible to do so? An urgent reply would be appreciated.

Thanks & Regards,
Swarup
 
D

David Squire

Swarup said:
Hello All,

Here is what the scenario is...

I have a perl script which runs for hours reading data and processing
it.

What i need to handle is if this perl process gets a kill process
either because of a manual kill or machine reboot etc. I should be
able to trap it, clean up if any resources and then EXIT.

Is it possible to do so?

Yes. See perldoc perlvar, and look for $SIG.
An urgent reply would be appreciated.

That is no way to talk to a newsgroup. When you are paying us you can
tell is it's urgent.


DS
 
A

anno4000

Swarup Baran said:
Hello All,

Here is what the scenario is...

I have a perl script which runs for hours reading data and processing
it.

What i need to handle is if this perl process gets a kill process
either because of a manual kill or machine reboot etc. I should be
able to trap it, clean up if any resources and then EXIT.

Look up %SIG in perlvar.
Is it possible to do so? An urgent reply would be appreciated.

"Urgent" and "Usenet" don't go together.

Anno
 
P

Paul Lalli

Swarup said:
What i need to handle is if this perl process gets a kill process
either because of a manual kill or machine reboot etc. I should be
able to trap it, clean up if any resources and then EXIT.

My understanding is that SIGKILL is very specifically *not* trappable.
Otherwise, it would be possible to write a program that simply cannot
be stopped (short of rebooting the machine). To wit:

$ perl -e'$SIG{$_} = sub { print "Caught SIG$_[0]\n" } for keys %SIG; 1
while 1' &
[1] 18338
$ kill -l
1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL
5) SIGTRAP 6) SIGABRT 7) SIGEMT 8) SIGFPE
9) SIGKILL 10) SIGBUS 11) SIGSEGV 12) SIGSYS
13) SIGPIPE 14) SIGALRM 15) SIGTERM 16) SIGUSR1
17) SIGUSR2 18) SIGCHLD 19) SIGPWR 20) SIGWINCH
21) SIGURG 22) SIGIO 23) SIGSTOP 24) SIGTSTP
25) SIGCONT 26) SIGTTIN 27) SIGTTOU 28) SIGVTALRM
29) SIGPROF 30) SIGXCPU 31) SIGXFSZ 32) SIGWAITING
33) SIGLWP 34) SIGFREEZE 35) SIGTHAW 36) SIGCANCEL
37) SIGLOST
$ kill -1 18338
Caught SIGHUP
$ kill -2 18338
Caught SIGINT
$ kill -3 18338
Caught SIGQUIT
$ kill -4 18338
Caught SIGILL
$ kill -9 18338
[1]+ Killed perl -e'$SIG{$_} = sub { print "Caught SIG$_[0]\n"
} for keys %SIG; 1 while 1'
$ ps -u plalli
PID TTY TIME CMD
17896 pts/4 0:00 bash
$
Is it possible to do so? An urgent reply would be appreciated.

That's just plain rude. If you need urgency, you hire a consultant.
If you want free help, you accept it whenever someone feels like giving
it to you.

Paul Lalli
 
A

A. Sinan Unur

My understanding is that SIGKILL is very specifically *not* trappable.
Otherwise, it would be possible to write a program that simply cannot
be stopped (short of rebooting the machine).

IIRC, On the *nix machines I have run, issuing the reboot command from the
prompt, results in the OS sending a SIGHUP, followed by a KILL. Presumably,
the SIGHUP is to allow processes to clean up.

I must admit, I am rather ignorant in these areas.

Sinan
 
A

A. Sinan Unur

(e-mail address removed)-berlin.de schreef:


But they can!

Isn't there a Perl-sponsor-site where people can Pal-Pay there
donations?
http://donate.perl-foundation.org/

Poster should mention the URL of the confirmed payment, to speed up the
answers.
;)

As an economist, I very much like that idea.

There would have to be a disclaimer of warranty specifically crafted for
this purpose, to reflect the fact that the donation really is just a
donation, not a fee-for-service. The donation would provide us with a
motivation to help the donor as quickly as we can if we know the answer.

Sinan
 
A

anno4000

A. Sinan Unur said:
IIRC, On the *nix machines I have run, issuing the reboot command from the
prompt, results in the OS sending a SIGHUP, followed by a KILL. Presumably,
the SIGHUP is to allow processes to clean up.

SIGHUP isn't universal, I think, but the procedure is: First send
a catchable signal and wait a bit, then SIGKILL all processes that
haven't got the hint.

Anno
 
X

xhoster

Indeed. My Camel book says: "Some signals can be neither trapped nor
ignored, such as the KILL and STOP signals."

I don't think the OP was considering SIGKILL specifically, but rather using
the generic term for any signal generated by (Perl or unix) kill commands,
which by default in fact or not SIGKILL. He would probably want to trap
HUP, INT, and QUIT, and TERM.

Xho
 
P

Peter J. Holzer

IIRC, On the *nix machines I have run, issuing the reboot command from the
prompt, results in the OS sending a SIGHUP, followed by a KILL. Presumably,
the SIGHUP is to allow processes to clean up.

I think you mean SIGTERM. SIGHUP is usually used to tell daemons to
reload their configuration.

hp
 
B

Ben Morrow

Quoth (e-mail address removed)-berlin.de:
SIGHUP isn't universal, I think, but the procedure is: First send
a catchable signal and wait a bit, then SIGKILL all processes that
haven't got the hint.

This is way OT, but any user processes will receive SIGHUP when their
session leader terminates. This is in POSIX. Process started from
/etc/rc (or whatever) will be stopped by the same mechanism, which
generally ends up sending a SIGTERM, though it may do other things. At
least linux sends TERM to all processes just before halt, before a final
KILL.

Ben
 
C

Charles DeRykus

Swarup said:
Hello All,

Here is what the scenario is...

I have a perl script which runs for hours reading data and processing
it.

What i need to handle is if this perl process gets a kill process
either because of a manual kill or machine reboot etc. I should be
able to trap it, clean up if any resources and then EXIT.

Normally then, someone will send you a TERM (and possibly follow that
with an untrappable KILL if ignored), so you could catch TERM at least:

$SIG{ TERM } = sub { ... # cleanup };


hth,
 
J

Josef Moellers

A. Sinan Unur said:
@h48g2000cwc.googlegroups.com:




IIRC, On the *nix machines I have run, issuing the reboot command from the
prompt, results in the OS sending a SIGHUP, followed by a KILL. Presumably,
the SIGHUP is to allow processes to clean up.

issuing the "reboot" command sends a message to the "init" process to
enter runlevel 6. Init then takes over and works through the shutdown
scripts, one of which tries to stop applications in a leveled approach,
as you describe it.
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top