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

Hi.

Is it possible to check, if a process with a special PID is still running? I cannot use 'ps' as I'm under Win2000 Server, and I don't have the possibility to install special tools, either.

Is there a solution for this problem only within perl (or with the help of perl modules)?

Thorsten
  • Comment on find out, if process with $PID exists (under Windows!)

Replies are listed 'Best First'.
Re: find out, if process with $PID exists (under Windows!)
by BrowserUk (Patriarch) on Apr 09, 2003 at 09:33 UTC

    Yes.

    if ( kill 0, $process_id) { print "process $process_id exists"; }

    This works equally well for both true processes and AS pseudo-processes. See perlfunc:kill with a first arg of 0 for more details.


    Examine what is said, not who speaks.
    1) When a distinguished but elderly scientist states that something is possible, he is almost certainly right. When he states that something is impossible, he is very probably wrong.
    2) The only way of discovering the limits of the possible is to venture a little way past them into the impossible
    3) Any sufficiently advanced technology is indistinguishable from magic.
    Arthur C. Clarke.
      Thanks!!!
Re: find out, if process with $PID exists (under Windows!)
by strat (Canon) on Apr 09, 2003 at 09:17 UTC
Re: find out, if process with $PID exists (under Windows!)
by AcidHawk (Vicar) on Apr 09, 2003 at 14:03 UTC

    Try something like

    #!/usr/bin/perl use strict; use warnings; use Win32::OLE qw( in ); use Win32::OLE::Variant; use Win32::Process; my $CLASS = "winmgmts:{impersonationLevel=impersonate}\\\\$ENV{COMPUTE +RNAME}\\Root\\cimv2"; my $WMI = Win32::OLE->GetObject( $CLASS ) || die "Cannot Get list of P +IDS\n"; foreach my $Proc ( sort {lc $a->{Name} cmp lc $b->{Name}} in( $WMI->In +stancesOf( "Win32_Process" ) ) ) { if ($Proc->{Name} eq "Whatever you are looking for.exe") { print "$Proc->{ProcessID} is STILL RUNNING\n"; # We Dont Want to KILL the PID ---Win32::Process::KillProcess( +$Proc->{ProcessID}, 5); # Other Processing Here..;-) } }
    If on the other hand you already know the PID you can simply run
    use Win32::process; Win32::Process::KillProcess($PID,5);
    Keep in mind though that $PID is a perlvar and if I ma correct returns the PID of perl.exe that is running your current script. Maybe you should change your var name..

      From the perlvar help - ($PID/$$) "The process number of the Perl running this script. You should consider this variable read-only, although it will be altered across fork() calls."

    Code Ripped from a working script I have running... This peice has not been tested on its own but should work..;-)

    Update: Thanks to BrowserUk for reminding me what the question really was..!

    -----
    Of all the things I've lost in my life, its my mind I miss the most.

      The OP didn't want to kill a specific process, only find out if it was still running.


      Examine what is said, not who speaks.
      1) When a distinguished but elderly scientist states that something is possible, he is almost certainly right. When he states that something is impossible, he is very probably wrong.
      2) The only way of discovering the limits of the possible is to venture a little way past them into the impossible
      3) Any sufficiently advanced technology is indistinguishable from magic.
      Arthur C. Clarke.

        OOPS... I did read that, but got carried away with ripping a peice of code that I had previously put together that I forgot what the question was... :-(

        My First piece of code still works just don't do the kill peice...;)

        -----
        Of all the things I've lost in my life, its my mind I miss the most.