Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Stop Command In Perl

by anshumangoyal (Scribe)
on Jul 12, 2012 at 16:20 UTC ( #981430=perlquestion: print w/replies, xml ) Need Help??
anshumangoyal has asked for the wisdom of the Perl Monks concerning the following question:

I am opening a pipe to run a command. After some time say 30 seconds, I want to send ctrl+c signal to this command to stop running. Here is my code:
if (! (open ($fileHandler, '-|', "$Command 2>&1"))) { die "Cannot Open Command\n"; } my $ofh = select $fileHandler; $| = 1; select $ofh; while (<$fileHandler>) { sleep 30; print $_; ## Here i want to stop the command }
Is there any good way to do that. I dont want to kill the process and this is performed on windows machine.

Replies are listed 'Best First'.
Re: Stop Command In Perl
by BrowserUk (Pope) on Jul 12, 2012 at 19:08 UTC

    See Re^3: trying to get timeout to work (easier with threads).

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    The start of some sanity?

Re: Stop Command In Perl
by monsoon (Pilgrim) on Jul 12, 2012 at 16:43 UTC
    Why not just exit the loop with last and close the handle right after? I don't get though why you need a loop at all in this case.
      Does closing the handler also kills the command?

        It would send a SIGPIPE which may or may not stop the command. This stuff is rather system specific and I'm not sure if what's the default on Windows. Just try.

        Otherwise if you really need a Ctrl-C to be sent: this form of open() returns the child's PID so you can use kill 2, $pid to send the signal.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://981430]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (9)
As of 2018-06-21 09:22 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (117 votes). Check out past polls.