Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

How to kill an external program

by Anonymous Monk
on Jan 13, 2003 at 15:07 UTC ( #226478=perlquestion: print w/replies, xml ) Need Help??

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

I am writing a script that takes a data file and runs an application (spider.exe) to update a database. The problem Iím having is that I donít know how to kill the application if it runs over a certain amount of time. I am able to start the application using the system command, and I can tell if the application finished processing because it generates an error file. I have also tried using proc::simple, but am having some interesting results. When starting the process, it always returns that the application has not started, yet it did start, and the poll function always returns the application is running even when it is not, and the kill method does not work at all. The server is running W2K, so the alarm function (as I understand from reading other threads) will not work either. I have also looked into IPC::Run, but am not sure if it will work (I could be wrong though) Does anyone have any suggestions on how to kill the application if it runs too long? Thanks

Things Iíve tried:
 system ("start k:\\spider\\spider.exeĒ); # works - donít know how to kill application
$spiderProc = Proc::Simple->new(); $spiderStatus = $spiderProc->start("k:\\spider\\spider.exe"); print ("spider start status: $ spiderStatus"); # returns start failed $spiderPoll = $spiderProc->poll(); print ("test poll: $spiderPoll"); # returns running $tempExitStat = $spiderProc->kill(); print ("killing: $tempExitStat"); # returns fill failed

Replies are listed 'Best First'.
Re: How to kill an external program
by boo_radley (Parson) on Jan 13, 2003 at 15:20 UTC
    If you're on windows ("spider.exe") you may have a better run of things if you use Win32::Process. You can set an amount of time for the process to run (->Wait) and kill it arbitrarily (->Kill) and so on.
Re: How to kill an external program
by batkins (Chaplain) on Jan 13, 2003 at 15:20 UTC
    You could fork off a child and call exec from that child. Then you could use the PID from fork to call kill. Thus:
    if($pid = fork() == 0) { exec("command"); } # this is all executed in the parent ... kill $pid;
Re: How to kill an external program
by BronzeWing (Monk) on Jan 13, 2003 at 19:33 UTC

    The following code works for me. It assumes the process returns before it's done (I think all Win32 GUI programs do that, but command line ones don't - which sort are you using?).

    my $PID = open my($Notepad), "|notepad.exe"; sleep 1; print "DIE!\n"; kill 9, $PID;

    Perl Monks do it more than one way.

      Thank you so much for your replies. I finally got it working with the Win32::Process. Here is what I came up withÖ Thanks again
      my $tempSleepMilSec = ($tempSleepSec * 1000); # amt of time to wait Win32::Process::Create($spiderProc, 'k:\\...\\spider.exe', "", 0, DETACHED_PROCESS, ".") || die ("can't create process: $!"); if ($spiderProc->Wait($tempSleepMilSec)) { print ("finished fine"); } else { print ("too long - killing"); my $tempKill = $spiderProc->Kill(0); }

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2023-05-29 09:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?