Handling ctrl + c in perl when script ran using setsid

463 Views Asked by At

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.

2

There are 2 best solutions below

0
gaganso On BEST ANSWER

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.

0
Borodin On

The setsid command starts your perl program in a new session with no controlling terminal. That leaves no way to interact with the process other than by process ID

This is pretty much the point of setsid in the first place. If you want to retain control of your program then you should run it without setsid