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

Hello! I need your advice, i need to kill a process (not a childprocess) from my perl script, i know the programs name but i don't know the PID. The other problem is that it should work under win too. Please help, thx :)

20050201 Edit by Corion: Changed title from 'kill'

Replies are listed 'Best First'.
Re: Kill a process by name
by xorl (Deacon) on Jan 31, 2005 at 20:31 UTC
Re: Kill a process by name
by punkish (Priest) on Jan 31, 2005 at 21:32 UTC
Re: Kill a process by name
by BrowserUk (Patriarch) on Jan 31, 2005 at 22:31 UTC

    If you have a reasonably recent version of Win that has the tasklist utitlity, then the following one-liner will do the job.

    CAUTION: As presented, it will kill all copies of programs which match the name supplied on the commandline.

    perl -le"kill 9,(split' ')[1]for grep/^$ARGV[0]$/i,split$/,`tasklist`" + taskname

    Examine what is said, not who speaks.
    Silence betokens consent.
    Love the truth but pardon error.
Re: Kill a process by name
by hsinclai (Deacon) on Jan 31, 2005 at 21:29 UTC
    You could use pslist and pskill, two standalone programs that work very well, and can be used also when you're not in the middle of a Perl script - they are mentioned here

    Also gnu.org has Windows ports of common Unix utilites (no need to install cygwin or other libraries - they're statically built) such as kill, ls, sed, grep, etc etc .. using these you can do lots more from the command line in Windows.

    Sorry I can't find the link, but if you search the gnu.org site you'll find a package of Windows ports in one of the Software sections .. I remember it was well hidden though..

    Update: Here is the link for the GNU ports (it wasn't on gnu/fsf, it's on SF) UnxUtils

Re: Kill a process by name
by jbrugger (Parson) on Jan 31, 2005 at 20:28 UTC
    You seem to run a unix like system since you're talking about the PID.
    Indeed use
    ps aux | grep processname
    under windows you could do the same using cygwin i think, and there is a 'kill' command for windows (external program something to be found here).
Re: Kill a process by name
by osunderdog (Deacon) on Jan 31, 2005 at 20:06 UTC

    Knowing what Operating System you are on would be a big help.

    If you're on Linux, killall might work for you.

    UPDATE: My mistake. He's looking for a windows command.


    "Look, Shiny Things!" is not a better business strategy than compatibility and reuse.

    20050201 Edit by Corion: Changed title from 'Re: kill'

Re: Kill a process by name
by ww (Archbishop) on Jan 31, 2005 at 20:21 UTC
    update: Should not have been...
    "written w/o access to man,"
    but think you may want
    ps -A |grep name
    even ps -A should give you the pid associated with the name under *n*x... but no clue how to make that work on windows.