http://qs321.pair.com?node_id=988007


in reply to Calling Control-C in linux - Perl equivalent

Assuming you know the process ID, then:

kill 2, $pid;

Hard-coding the constant 2 may cause you trouble on non-Linux systems though. Better to use the constant defined in POSIX...

kill(POSIX::SIGINT, $pid);

If you have multiple processes to kill, kill takes a list...

kill(POSIX::SIGINT, @murder_victims);

Note that SIGINT is just a request for the process to end. The process can catch the signal and deliberately ignore it. If you want to force the process to die, then use SIGKILL instead.

SIGINT though is exactly what Ctrl+C does in the terminal. The terminal sees that you've pressed Ctrl+C and sends a SIGINT to the current foreground job.

perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

Replies are listed 'Best First'.
Re^2: Calling Control-C in linux - Perl equivalent
by Freezer (Sexton) on Aug 17, 2012 at 17:06 UTC
    I am struggling to use SIGINT. Because when I try to install it cpan says that it came with the dist. and that force install would have to be used. However when I try to run the program:
    Can't locate POSIX/SIGINT.pm in @INC (@INC contains:
      use POSIX; say POSIX::SIGINT;
      You seem to be trying to include the module POSIX::SIGINT, that is not what you want to do.
Re^2: Calling Control-C in linux - Perl equivalent
by Freezer (Sexton) on Aug 17, 2012 at 16:08 UTC
    When I run the program it does present a number that might be the process id. How do I use Perl to get the process id? Presumably when I press control-C that is only killing the specific process.