3

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.

gaganso
  • 2,914
  • 2
  • 26
  • 43
Niraj Nandane
  • 1,318
  • 1
  • 13
  • 24
  • 2
    Note it will handle a SIGINT sent to it (say, using `kill -INT $pid`), but the whole point of `setsid` is to detach the process from the terminal so things like Ctrl-C doesn't affect it. – ikegami May 18 '16 at 14:39

2 Answers2

5

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.

gaganso
  • 2,914
  • 2
  • 26
  • 43
4

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

Borodin
  • 126,100
  • 9
  • 70
  • 144