Stopping all node processes in a directory aside from one I'm just starting

I'm using sublime text 2's build systems to aid development of my mongodb + node.js server, which is really handy as it enables me to test my code without having to keep going back and to to the terminal. The downside is that it's very easy to absent-mindedly leave multiple node processes running in the background, which sometimes causes clashes when one of them is using a port I need in order to test another module.

Is there some way I can stop all node processes running within a given directory whenever I start a new process from that directory? A bash script or similar?

Try something like this:

ps auxwwwe | egrep " [n]ode .+ PWD=$PWD" | awk '{ print $2 }' | xargs kill

This does the following:

  1. Uses ps to get the full path to your processes.
  2. Searches for all processes started with the node command in the present working directory (you can modify this with an absolute path, another environment variable, etc.).
  3. Finds the 2nd column: the pid (use awk or nawk depending on your system).
  4. Runs kill on each pid.