Perl Signal Capture

I have a simple Perl script that just prints a line of text in stdout. What I want to execute is that while this script is running, if I (or someone else) gives a signal to this process to stop it, I want it to capture this signal and exit it cleanly. The code I have is as follows

#!/usr/bin/perl -w

$| = 1;
use sigtrap 'handler' => \&sigtrap, 'HUP', 'INT','ABRT','QUIT','TERM';
while(1){
 print "Working...\n";
 sleep(2);
}
sub sigtrap(){
 print "Caught a signal\n";
 exit(1);
}

Although this works well when I actually press ctrl-c from the command line if I issue

kill -9 <pid>

He is just dying. How do I get him to do something before exiting? My general idea is to use this framework to capture when this script dies on the server due to a server reboot for maintenance or failure.

Thanks in advance

+5
source share
3

№ 9 (SIGKILL) . Unix .

. , . TERM (, , , script /etc/init.d)). , , SIGKILL.

, , TERM script /etc/init.d, , .

: Daemon:: Control init script.

+11

. Ctrl-C TERM INT, , , . kill -9, , 9, KILL. , , , .

+4

As far as I know, you cannot capture kill -9. Try it instead kill <pid>.

+3
source

All Articles