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
$| = 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
source
share