• Home
  • Blog
  • Wiki
  • Projects
  • Contact Us






Windows Tricks For Whacking Lingering Java Processes That Tie Up Your Ports and Cause Other Mischief

November 8th, 2009 By admin

When forced to use Windoze, I absolutely depend on Cygwin to keep from going completely insane. But there are cases where Cygwin just doesn’t quite work like good ol’ Linux/Unix, and in these cases you need to do a little extra work. One such case occurs with the ‘kill’ command. Some Windows processes just refuse to do die even after you kill -9 them.

[[Update: please see comment by Andan below.

Better solution than mine !

I recently noticed that my solution does not work on XP, although it works on Vista. So this is all the more reason to use pskill, as suggested by Andan.

]]

At one point, I was relying on a script that used the kill command to whack background Java processes that were hogging ports and preventing new instances of server applications from launching.

Unaware that kill wasn’t working as expected, I spent a lot of time on wild goose chases. Finally I realized I couldn’t rely on Cygwin’s kill, and I started using the Windows task manager to list and kill my Java processes. This worked, but was quite tedious.

After researching other approaches, I found that the Windows ‘taskkill’ command does exactly does whats’ required, with no mercy. It works on Vista, and (AFAIK) earlier MS platforms as well.

Using taskkill, I wrote the bash script below, with two aliases

  • alias javaKill=”$HOME/scripts/DEVENV/killProc java”
  • alias foxKill=”$HOME/scripts/DEVENV/killProc firefox”

Killing firefox required an additional loop in the script, as you can see from the comments.

#/bin/bash

procText=$1

# Loop through the "find and kill" procedure multiple times as a
# special case for Firefox. You have to shoot Firefox in the head 1
# time for every Window that is currently up.  The mayhem ceases
# only when no more 'firefox' processes are found.

 while [ true ] ; do
    found=` ps -aW | grep $procText |  sed -e's/^\s*\([0-9][0-9]*\).*/\1/' `

    if [ "$found" = "" ] ; then
        break
    else
        echo more to go

    fi 

    echo  processes found: $found

    for  process in $found ; do
        echo killing process: $process
         cmd /X /C "taskkill /F /PID $process"
    done

done
Share:
  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • blogmarks
  • description
  • Furl
  • LinkedIn
  • Live
  • MySpace
  • Netvibes
  • Netvouz
  • NewsVine
  • Ping.fm
  • Pownce
  • Reddit
  • StumbleUpon
  • Technorati
  • Tumblr
  • TwitThis
  • Yahoo! Buzz
  • YahooMyWeb

Posted in Cygwin, Java | 2 Comments »




2 Responses to “Windows Tricks For Whacking Lingering Java Processes That Tie Up Your Ports and Cause Other Mischief”

  1. By anjan bacchu
    on November 8th, 2009

    hi there,

    I use sysinternals ps tools like pskill.
    to kill all instances of java, you just need to type
    pskill java — lo and behold all java processes will die instantaneously.

    I’ve rarely seen pskill fail.

    BR,
    ~A


  2. By Mike
    on November 18th, 2009

    Why not just setup a VM running one of the Linux flavors and do your development in there? Use a shared folder to move things between the two environments (host and guest).


Leave a Reply

  • Categories

    • Build Automation
      • Maven
    • Development Environment Tricks
      • Cygwin
    • Dynamic Languages
      • Groovy
        • Grails
    • Java
    • JQuery
    • Test Automation
      • Selenium
    • Uncategorized

  • Archives

    • June 2010
    • March 2010
    • January 2010
    • December 2009
    • November 2009
    • October 2009
    • September 2009
    • July 2009
    • June 2009
    • May 2009
    • April 2009
    • March 2009
  • Meta

    • Log in
    • RSS
    • Comments RSS
    • Valid XHTML
    • XFN
    • WP