Search code examples
perlcentossetsid

Handling ctrl + c in perl when script ran using setsid


My Perl script looks like this

A.pl

#!/usr/bin/perl
system("perl ctrlc.pl");

ctrlc.pl

sub signal_handler {
    print "Niraj";
}

$SIG{INT} = \&signal_handler;
print "Enter number";

my $no1 = <>;

When I run perl A.pl and press Ctrl-C it is detecting and printing "Niraj". But when I run setsid perl A.pl , it is not detecting Ctrl-C.


Solution

  • setsid creates a new session.

    The SIGINT signal is sent to the foreground process group of a session associated to the tty. Since the process A.pl is now in a different session, effectively in a different process group, the signals are not received by A.pl.