How to catch and handle a signal in Perl

Last updated on June 22, 2020 by Dan Nanni

Question: I need to handle an interrupt signal by using a custom signal handler in Perl. In general, how can I catch and handle various signals (e.g., INT, TERM) in a Perl program?

As an asynchronous notification mechanism in the POSIX standard, a signal is sent by an operating system to a process to notify it of a certain event. When a signal is generated, the target process's execution is interrupted by an operating system, and the signal is delivered to the process's signal handler routine. One can define and register a custom signal handler or rely on the default signal handler.

In Perl, signals can be caught and handled by using a global %SIG hash variable. This %SIG hash variable is keyed by signal numbers, and contains references to corresponding signal handlers. Therefore, if you want to define a custom signal handler for a particular signal, you can simply update the hash value of %SIG for the signal.

Here is a code snippet to handle interrupt (INT) and termination (TERM) signals using a custom signal handler.

$SIG{INT}  = &signal_handler;
$SIG{TERM} = &signal_handler;

sub signal_handler {
    print "This is a custom signal handlern";
    die "Caught a signal $!";
}

Other valid hash values for %SIG are IGNORE and DEFAULT. When an assigned hash value is IGNORE (e.g., $SIG{CHLD}='IGNORE'), the corresponding signal will be ignored. Assigning 'DEFAULT' hash value (e.g., $SIG{HUP}='DEFAULT') means that we will be using a default signal handler.

Support Xmodulo

This website is made possible by minimal ads and your gracious donation via PayPal or credit card

Please note that this article is published by Xmodulo.com under a Creative Commons Attribution-ShareAlike 3.0 Unported License. If you would like to use the whole or any part of this article, you need to cite this web page at Xmodulo.com as the original source.

Xmodulo © 2021 ‒ AboutWrite for UsFeed ‒ Powered by DigitalOcean