Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Re: Calling Control-C in linux - Perl equivalent

by tobyink (Canon)
on Aug 17, 2012 at 15:49 UTC ( #988007=note: print w/replies, xml ) Need Help??

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/ 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.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://988007]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2022-05-17 18:43 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (68 votes). Check out past polls.