Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Kill a process in Perl

by uzers (Initiate)
on Jul 11, 2009 at 06:07 UTC ( #779114=perlquestion: print w/ replies, xml ) Need Help??
uzers has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,
I am trying to kill an external process called out in Perl after certain seconds.
I am using Windows.
I want to call out "a.exe" in perl in a new thread (so that the main perl program can go on). Then I want to kill the "a.exe" after 30 seconds before a.exe naturally ends by itself which usually takes 10mins ?
I think I have to kill it by process ID of "a.exe" but not the process name, because I will run many a.exe at the same time, and I only want the perl to kill its own a.exe. But, I have no idea how to get process ID also...
Any idea to do the mentioned task?
Please kindly help me. Thanks in advance!
Uzers.

Comment on Kill a process in Perl
Re: Kill a process in Perl
by GrandFather (Cardinal) on Jul 11, 2009 at 06:17 UTC

    If you are launching the process within the script then you can use Win32::Process to launch and manage the script. You don't need any threading or forking to support that.


    True laziness is hard work
      In fact, if you have the pid of the process that you want to kill, you can kill it with Win32::Process::KillProcess($pid, $exitcode), irrespcective of how the process was started.

      Cheers,
      Rob

        True, but finding the pid for a process is a right pita! Every time I want to do it (about 11 months apart on average) it seems to take an hour of trawling through MSDN to find what needs to be done. Then after 10 months I forget how to do it.


        True laziness is hard work
Re: Kill a process in Perl
by BrowserUk (Pope) on Jul 11, 2009 at 07:03 UTC

    #! perl -slw use strict; use threads; use threads::shared; $|++; my $pid :shared; async { $pid = system 1, 'perl', q[-E"sleep 600; say 'Done!'"]; sleep 30; kill 2, $pid; undef $pid; }->detach; my $c = 0; ## get on with the rest of your program while( sleep 1 ) { print ++$c, ' Is it done yet?'; last unless $pid; } __END__ C:\test>\perl32\bin\perl.exe junk3.pl 1 Is it done yet? 2 Is it done yet? 3 Is it done yet? ... 27 Is it done yet? 28 Is it done yet? 29 Is it done yet? 30 Is it done yet? Terminating on signal SIGINT(2) 31 Is it done yet?

    If you don't need to do anything else whilst you wait for it, then a simple:

    #! perl -slw use strict; my $pid = system 1, 'perl', q[ -E"sleep 600; say 'Done!'"]; sleep 30; kill $pid, 2; ## rest of program

    will do it.


    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.
Re: Kill a process in Perl
by uzers (Initiate) on Jul 11, 2009 at 07:41 UTC
    Thank you so much for your prompt replies and good suggestions, GrandFatherm, syphilis and BrowserUk!
    Yes, I followed your suggestion using Win32::Process, and it works perfectly well in my case (with Create and Kill). Thanks a lot!

    syphilis mentioned using KillProcess. Is there any way to screen/read the current processes running in Windows to get their pid in Perl?
    Thanks!
    Uzers
      Is there any way to screen/read the current processes running in Windows to get their pid in Perl?

      Yes. Win32::Process::Info will get you most the information you need to do that. The missing part is the command line (parameters) if you need those to distinguish between concurrent instances. Though you may be able to determine that by using the ->Subprocesses() method if you know the pid of the process that started the instance you are looking for.


      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.
Re: Kill a process in Perl
by eyepopslikeamosquito (Canon) on Jul 11, 2009 at 09:06 UTC

    Generally, I prefer Win32::Job to Win32::Process for this type of thing. Notice that, unlike Win32::Process, Win32::Job allows you to reliably kill a process and all its children by launching the process in a job. Example code that uses Win32::Job can be found in Re: Timing Windows commands.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (9)
As of 2014-08-27 21:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (253 votes), past polls