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

how to kill subprocess when it goes longer than 20 seconds?

by echoangel911 (Sexton)
on Mar 28, 2007 at 15:11 UTC ( [id://607045]=perlquestion: print w/replies, xml ) Need Help??

echoangel911 has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I am launching a command like this:
open (SYS, "/bin/tcsh $tempfilename | ") or die "$!"; while (<SYS>) { print "[DEBUG] $_ <br>"; } close SYS;
Inside the tempfilename, the environment is setup and my tool is called. If someone else is using my tool due to license restrictions, the program will hang thus causing my cgi perl script to hang. Is there anyway to put a timer on this to exit and then continue on?

Replies are listed 'Best First'.
Re: how to kill subprocess when it goes longer than 20 seconds?
by ikegami (Patriarch) on Mar 28, 2007 at 15:17 UTC

    open returns the child's PID on success. You can use kill along with this PID to end the process.

    That leaves you with waiting for 20 seconds. alarm is one way to go.

      is this correct?
      my $temppid = open (SYS, "/bin/tcsh $tempfilename | ") or die "$!"; kill $temppid if (alarm (5)); while (<SYS>) { print "[DEBUG] $_ <br>"; } close SYS;

        Check alarm documentation. There's a lot of examples how you do that.

        Igor 'izut' Sutton
        your code, your rules.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (9)
As of 2024-04-18 11:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found