Graceful termination of process


This process will catch both the INT and the TERM signals and in both cases it will flip a flag and based on that flag it will stop he program. INT is received when the user presses Ctrl-C or if using a another terminal the user sends the kill -2 signal. TERM is kill -15.

When we run the program it will print out its process ID and instructions how to stop it.

kill -15 810344 or press Ctrl-C to stop

So Ctrl-C instead of just killing the process in mid-operation it will tell the process to "please stop" and the loop will finish whatever it was doing and exit the program.


examples/signals/graceful_termination.pl
#!/usr/bin/env perl
use strict;
use warnings;

print "kill -15 $$    or press Ctrl-C    to stop\n";
my $continue = 1;
$SIG{TERM} = sub {
    print "TERM received\n";
    $continue = 0;
};

$SIG{INT} = sub {
    print "INT received\n";
    $continue = 0;
};

while ($continue) {
    if (open my $fh, '>>', 'process.log') {
        print $fh scalar localtime();
        print $fh "\n";
    }
    sleep 1;
}