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

Hello most esteemed PerlMonks, I am currently coding a simple script which kills a particular process, and then once another condition is met, restarts that same process.
My problem is the following code doesn't seem to kill the process :
$pid= $Proc->{ProcessID}; Win32::Process::Open($p_obj, $pid, 1); $p_obj->GetExitCode($exitcode); Win32::Process::KillProcess($pid, $exitcode);
What I end up with is a correct process ID, and an exitcode, but the process does not die ! There are
no errors returned either ...
Am I doing something wrong here ?
Thanks a lot in advance !
Travis Weir
P.S. I am a beginner with Perl ... Please go easy
on me ... :)

Replies are listed 'Best First'.
Re: Win32::Process::KillProcess
by Ninthwave (Chaplain) on Dec 18, 2003 at 21:49 UTC

    This works for me taking the example of creating a process in the documentation for Win32::Process

    #!/usr/bin/perl use strict; use Win32::Process; my $ProcessObj; Win32::Process::Create($ProcessObj, "C:\\winnt\\system32\\notepad.exe", "notepad temp.txt", 0, NORMAL_PRIORITY_CLASS, ".")|| die "Oops"; my $exit_code = 1; my $wait=<STDIN>; chomp $wait; $ProcessObj->Kill($exit_code);

    Some tests in your code would be to print out some of the variables you are using and seeing what there values are so you know what is happening with your values. You are calling values for exit code that will not exist until the program exits.

    Some references:
    Using OLE
    Win32::Process::Info
    I would suggest using Win32::Process::Info to get the process ID and using Win32::Process to kill the process. Specifically look at GetProcInfo (); and getting the name to search for the pid of a particualar application.

    "No matter where you go, there you are." BB
      either this :
      Win32::Process::KillProcess($pid, 1);
      or this :
      $p_obj->Kill($exitcode);
      ... do not work. The process that I am trying to kill is not touched, even though log traces state that the kill part of my code has been reached. Is there an error code that either of these functions return ?
      I'm coding this on a Windows 2003 Server machine, and the only thing that seems to work is :
      system 'fkill ' . $pid;
      I am at a total loss here.

        #!/usr/local/bin/perl use strict; use warnings; use Win32::Process; use Win32::Process::Info; my $Process_Obj = Win32::Process::Info->new (undef , "NT"); my @info = $Process_Obj->GetProcInfo (); my $Pid; my $Name; my %Process; for my $i ( 0 .. $#info ){ $Pid = $info[$i]{"ProcessId"}; $Name = $info[$i]{"Name"}; print "$Pid \t $Name \n"; $Process{$Pid} = $Name; } print "What program do you want to kill?\n"; my $exit_code = 1; my $kill = <STDIN>; chomp $kill; Win32::Process::KillProcess($kill, $exit_code)
        Update: Simplified for loop to only focus on name and Pids
        This works for me. Now I broke down the data structure to show the important bits of Win32::Process::Info . And I am getting my PID from STDIN but you can use the Name and Process ID keys from the Win32::Process::Info to identify the process you want to kill and then call Win32::Process::KillProcess to end it. But you need to make sure in that case that the PID is a running process. I don't know how you are determining the PID programatically on how this will work.

        If you give more specifics I can show more specific examples this is done in a way to show where the PID can come from.

        Update: I am trying to simplify the id, in your intial post you did not show where your PID was coming from. For this to work we need to know how you are determining the pid to kill. In this example I am printing a list and having the user choose but the same information can be used programatically to kill processes. I tend to search for a name and kill all instances of that name. But again this is just an example. If you need any more info just describe what you are doing with a bit more detail and I will try to help.

        "No matter where you go, there you are." BB
Re: Win32::Process::KillProcess
by traveler (Parson) on Dec 18, 2003 at 21:31 UTC
    You are combining two things. You can use something like Win32::Process::KillProcess(5000,1); to kill process 5000 (or the numbers can be in variables). Since you did an Open using Win32::Process::Open you can use $p_obj->Kill($exitcode); (or use a constant). Do not call GetExitCode -- it is used to return the code after the process has exited.

    HTH, --traveler