I'd like to restart one of many Node.js processes I have running on my server. If I run ps ax | grep node
I get a list of all my Node proccesses but it doesn't tell me which port they're on. How do I kill the one running on port 3000 (for instance). What is a good way to manage multiple Node processes?

- 1,011
- 4
- 16
- 28
-
possible duplicate of [What processes are using which ports on unix?](http://stackoverflow.com/questions/126002/what-processes-are-using-which-ports-on-unix) – blu Oct 29 '12 at 21:06
-
@blu yes you're right. I didn't see that one there. – Pardoner Oct 29 '12 at 22:00
4 Answers
If you run:
$ netstat -anp 2> /dev/null | grep :3000
You should see something like:
tcp 0 0 0.0.0.0:3000 0.0.0.0:* LISTEN 5902/node
In this case the 5902
is the pid. You can use something like this to kill it:
netstat -anp 2> /dev/null | grep :3000 | awk '{ print $7 }' | cut -d'/' -f1 | xargs kill
Here is an alternative version using egrep
which may be a little better because it searches specifically for the string 'node':
netstat -anp 2> /dev/null | grep :3000 | egrep -o "[0-9]+/node" | cut -d'/' -f1 | xargs kill
You can turn the above into a script or place the following in your ~/.bashrc
:
function smackdown () {
netstat -anp 2> /dev/null |
grep :$@ |
egrep -o "[0-9]+/node" |
cut -d'/' -f1 |
xargs kill;
}
and now you can run:
$ smackdown 3000

- 63,632
- 11
- 148
- 146
-
Glad to be of assistance. :) I added an alternative (possibly safer) version of the command and a `.bashrc` function to the answer. – David Weldon Oct 29 '12 at 23:43
-
put the smackdown function in my bashrc but it just outputs `Usage: kill pid ... Send SIGTERM to every process listed. kill signal pid ... Send a signal to every process listed. kill -s signal pid ... Send a signal to every process listed. kill -l List all signal names. kill -L List all signal names in a nice table. kill -l signal Convert between signal numbers and names.` – Pardoner Oct 30 '12 at 04:35
-
Did the egrep version work for you on the command line? You can try debugging the .bashrc function just by removing commands from the bottom up (start by removing the xargs call) and seeing what gets printed. remember to `source ~/.bashrc` after each change. – David Weldon Oct 30 '12 at 04:59
-
A little addition: if you're running on a sensitive port like 80, you might need to add `sudo` before the command. – Michael Litvin Aug 26 '15 at 08:06
-
And also, you might to add the pipe `| uniq` at the end, because the line might repeat several times. – Michael Litvin Aug 26 '15 at 09:18
This saved me a lot of time:
pkill node

- 159
- 1
- 3
-
-
Although this does not answer the entire question precisely, practically I would only run one instance of node per server. Port number becomes irrelevant in that case. – Naveed Ul Islam Sep 04 '18 at 18:23
A one-liner is
lsof -n -i:5000 | grep LISTEN | awk '{ print $2 }' | uniq | xargs -r kill -9
You only need the sudo if you are killing a process your user didn't start. If your user started the node process, you can probably kill it w/o sudo.
Good luck!

- 15,473
- 7
- 79
- 96
Why not a simple fuser
based solution?
If you don't care about whether the process using port 3000 is node, it could be as simple as
fuser -k -n tcp 3000
If you wan't to be sure you don't kill other processes, you could go with something like
PID="$(fuser -n tcp 3000 2>/dev/null)" \
test "node"="$(ps -p $PID -o comm=)" && kill $PID

- 1,289
- 13
- 18