in reply to kill a process with process name

Don't use fork on Windows. Windows emulates fork using a thread. Therefore, exec cannot replace the "child process" with the program exec'd. Therefore the "$pid" for the child does not refer to that exec'd process, but rather is actually a thread handle. So killing that "$pid", only kills the thread, and not the exec'd program.

A far simpler mechanism of running an asynchronous process on windows is to use system with a first parameter of '1'. It will the return the pid of the program started and kill will act on it directly:

perl -wle"my $pid= system 1,'c:/windows/system32/calc.exe';sleep 3;kil +l 9,$pid"

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.
RIP an inspiration; A true Folk's Guy

Replies are listed 'Best First'.
Re^2: kill a process with process name
by Anonymous Monk on Apr 14, 2010 at 12:37 UTC
    The actual script uses cmd in backticks `command 2>&1 >NUL` instead of exec. While replacing with system do I need to take care of anything?

      No. As you are redirecting all output to the nul device, you woudl have been better using system anyway. Backticks is only for when you want to retrieve the output.


      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.
        Instead of fork we're trying to use Win32::Job. I want some help on the watch().
        I want to kill the spawned process after a maximum timeout / if log file not updated. I tried watch(\&handler, maxTimeout) where handler() monitors the log file, but it dosen't work as I expected.