问题描述
我想重新启动我在服务器上运行的许多Node.js进程之一.如果运行ps ax | grep node
,我会得到所有Node进程的列表,但不会告诉我它们在哪个端口上.如何杀死运行在端口3000上的那个(例如).什么是管理多个Node进程的好方法?
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?
推荐答案
如果您运行:
$ 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
在这种情况下,5902
是pid.您可以使用类似的方法将其杀死:
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
这里是使用egrep
的替代版本,可能会更好一些,因为它专门搜索字符串'node':
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
您可以将以上内容转换为脚本,或将以下内容放入您的~/.bashrc
:
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;
}
现在您可以运行:
$ smackdown 3000
这篇关于如何找出哪个Node.js pid在哪个端口上运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!