1

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?

tshepang
  • 12,111
  • 21
  • 91
  • 136
wheresrhys
  • 22,558
  • 19
  • 94
  • 162
  • Do you want to stop all node processes in that directory, or do you want to stop all within a certain port range, or both? [This question](http://stackoverflow.com/questions/13129464/how-to-find-out-which-node-js-pid-is-running-on-which-port/) may be useful to start with. – David Weldon Feb 04 '13 at 18:40
  • The ones that cause the problems are the ones listening to a port (which I do a `lsof -i:portnum -t` followed by `kill` to stop) but I'd like to solve the underlying problem of orphan node processes in general – wheresrhys Feb 04 '13 at 18:49

1 Answers1

0

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.
David Weldon
  • 63,632
  • 11
  • 148
  • 146