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

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.

Comment on Stop Command In Perl
Download Code
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.

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?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://981430]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2014-12-28 04:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (178 votes), past polls