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

I have a PID, and would like to know whether a process with this PID is running on my host. What is a good way to do this?

On Unix, I would simply do a kill(0,$pid), but - as is explained in perlport - this does not work on Windows. I could shell out and call the Windows utility wmic, which is neither elegant, not safe, since it might happen that the wmic process itself gets, by chance, the PID I am going to check. I could also use Win32::Process::Info, but the plenty of "warnings" and "caveats" in the description of this module frightens me, to be honest.

Any other ideas? BTW, any solution must work on Windows XP and Windows 7.

-- 
Ronald Fischer <ynnor@mm.st>

Replies are listed 'Best First'.
Re: Simple way to find on Windows, whether a process is running
by BrowserUk (Patriarch) on Sep 09, 2011 at 14:03 UTC

    From the perlport manpage you referenced:

    As in Unix, if $sig is 0 and the specified process exists, it returns true without actually terminating it. (Win32)

    And from experience, kill 0, $pid works fine. It doesn't do it using signals, but the emulation is good.


    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.

      At least on Windows 7, this is not true anymore. I tried it with various PIDS - no matter, whether the process exists or not, kill always returned true :-( However, this is Perl 5.8.8. This Perl was implemented before Windows 7, so it could be that a newer Perl version has a better "kill".

      -- 
      Ronald Fischer <ynnor@mm.st>

        From the Active State change log:

        Bug Fixes and Changes since build 821

        ...

        kill(0, $pid) was always returning TRUE in build 820 on Windows. It now returning the correct values again (bug 67519).

        Upgrade. Works okay on AS 5.8.9:

        c:\test>\perl32\bin\perl -le"print $]; print kill 0, $_ for 3844, 4844 +" 5.008009 1 0

        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.