如何查找侦听/使用我的tcp端口的进程?我在mac os x上.
有时,在崩溃或一些错误后,我的rails应用程序锁定端口3000.我找不到它使用ps -ef ...我怎么找到愚蠢的东西并杀死它,残忍......?
做的时候
rails server
Run Code Online (Sandbox Code Playgroud)
我明白了
已经在使用的地址 - bind(2)(Errno :: EADDRINUSE)
2014年更新:
要完成以下某些答案:执行kill命令后,可能需要删除pid文件 rm ~/mypath/myrailsapp/tmp/pids/server.pid
gho*_*g74 2641
你可以试试 netstat
netstat -vanp tcp | grep 3000
Run Code Online (Sandbox Code Playgroud)对于macOS El Capitan和更新版(或者如果您的netstat不支持-p),请使用lsof
sudo lsof -i tcp:3000
Run Code Online (Sandbox Code Playgroud)对于Centos 7使用
netstat -vanp --tcp | grep 3000
Run Code Online (Sandbox Code Playgroud)Fil*_*nov 1662
找:
sudo lsof -i :3000
Run Code Online (Sandbox Code Playgroud)
杀:
kill -9 <PID>
Run Code Online (Sandbox Code Playgroud)
Aus*_*tin 185
上面没有任何东西对我有效.有我经验的任何人都可以尝试以下(为我工作):
跑:
lsof -i :3000 (where 3000 is your current port in use)
Run Code Online (Sandbox Code Playgroud)
然后检查报告的PID的状态:
ps ax | grep <PID>
Run Code Online (Sandbox Code Playgroud)
最后,"用它来做":
kill -QUIT <PID>
Run Code Online (Sandbox Code Playgroud)
Zle*_*ni 139
使用端口3000提取进程的PID并使其终止的单行程序.
lsof -ti:3000 | xargs kill
Run Code Online (Sandbox Code Playgroud)
-t标志从lsof输出中删除除PID之外的所有内容,从而可以轻松地将其删除.
Abh*_*mar 70
最简单的解决方案:
对于单端口:
kill $(lsof -ti:3000) #3000 is the port to be freed
Run Code Online (Sandbox Code Playgroud)
使用单行命令杀死多个端口:
kill $(lsof -ti:3000,3001) #3000 and 3001 are the ports to be freed
Run Code Online (Sandbox Code Playgroud)
lsof -ti:3000
82500(进程ID)
lsof -ti:3001
82499
lsof -ti:3001,3000
82499 82500
杀死$(lsof -ti:3001,3000)
在单个命令中终止82499和82500进程。
在package.json脚本中使用此命令:
"scripts": {
"start": "kill $(lsof -ti:3000,3001) && npm start"
}
ale*_*xzg 58
在您的中.bash_profile,为terminate3000流程创建快捷方式:
terminate(){
lsof -P | grep ':3000' | awk '{print $2}' | xargs kill -9
}
Run Code Online (Sandbox Code Playgroud)
然后,$terminate如果它被阻止,请打电话.
Tad*_*egn 56
要强制终止类似的进程,请使用以下命令
lsof -n -i4TCP:3000
Run Code Online (Sandbox Code Playgroud)
其中3000是进程运行的端口号
这将返回进程ID(PID)并运行
kill -9 "PID"
Run Code Online (Sandbox Code Playgroud)
将PID替换为运行第一个命令后得到的数字
Bin*_* Ho 49
杀死多个端口。
$ npx kill-port 3000 8080 8081
Process on port 3000 killed
Process on port 8080 killed
Process on port 8081 killed
Run Code Online (Sandbox Code Playgroud)
希望这有帮助!
Kri*_*ris 36
lsof -P | grep ':3000' | awk '{print $2}'
Run Code Online (Sandbox Code Playgroud)
这将为您提供在MacOS上测试的pid.
YBa*_*hia 28
杀死端口进程的方法之一是使用python库:freeport(https://pypi.python.org/pypi/freeport/0.1.9).安装完成后,只需:
# install freeport
pip install freeport
# Once freeport is installed, use it as follows
$ freeport 3000
Port 3000 is free. Process 16130 killed successfully
Run Code Online (Sandbox Code Playgroud)
JE4*_*E42 27
在OS-X El Captain上的命令行执行:
kill -kill `lsof -t -i tcp:3000`
Run Code Online (Sandbox Code Playgroud)
lsof的Terse选项只返回PID.
小智 21
找到并杀死:
这个单一的命令行很简单,工作正常.
kill -9 $(lsof -ti tcp:3000)
Run Code Online (Sandbox Code Playgroud)
smo*_*oth 14
实现这一目标的可能方法:
最佳
top命令是查看系统资源使用情况的传统方法,并查看占用最多系统资源的进程.Top显示进程列表,其中顶部使用最多的CPU.
PS
ps命令列出正在运行的进程.以下命令列出了系统上运行的所有进程:
ps -A
Run Code Online (Sandbox Code Playgroud)
您还可以通过grep管道输出以搜索特定进程,而无需使用任何其他命令.以下命令将搜索Firefox进程:
ps -A | grep firefox
Run Code Online (Sandbox Code Playgroud)
将信号传递给程序的最常用方法是使用kill命令.
kill PID_of_target_process
Run Code Online (Sandbox Code Playgroud)
lsof的
所有打开文件的列表以及打开它们的进程.
lsof -i -P | grep -i "listen"
kill -9 PID
Run Code Online (Sandbox Code Playgroud)
要么
lsof -i tcp:3000
Run Code Online (Sandbox Code Playgroud)
Cal*_*ene 10
我为此做了一个小功能,将它添加到您的 rc 文件(.bashrc,.zshrc或其他)
function kill-by-port {
if [ "$1" != "" ]
then
kill -9 $(lsof -ni tcp:"$1" | awk 'FNR==2{print $2}')
else
echo "Missing argument! Usage: kill-by-port $PORT"
fi
}
Run Code Online (Sandbox Code Playgroud)
然后你可以输入kill-by-port 3000杀死你的 rails 服务器(用 3000 代替它运行的任何端口)
失败了,你总是可以kill -9 $(cat tmp/pids/server.pid)从 rails 根目录输入
lsof -i tcp:port_number -将列出在该端口上运行的进程
kill -9 PID -将杀死进程
在你的情况下,它将是
lsof -i tcp:3000 从您的终端中找到过程的PID
kill -9 PID
kill -9 $(lsof -ti:3000)
Run Code Online (Sandbox Code Playgroud)
始终在 macOS 上为我工作。
如果您正在处理 node.js 项目,则可以将其添加到 package.json 脚本中,例如;
"scripts": {
...
"killme": "kill -9 $(lsof -ti:3000)",
...
},
Run Code Online (Sandbox Code Playgroud)
然后
npm run killme
Run Code Online (Sandbox Code Playgroud)
——
此外,如果您想为 macOS 添加系统范围的别名,请按照以下步骤操作;
导航到您的主目录:
光盘~
使用 nano 或 vim 打开 .bash_profile 或 zsh 配置文件:
vi .bash_profile
添加别名(按 i):
别名 killme="kill -9 $(lsof -ti:3000)"
保存存档
重启终端
输入killme终端
当然,您可以将端口 3000 更改为您想要的。
添加到~/.bash_profile:
function killTcpListen () {
kill -QUIT $(sudo lsof -sTCP:LISTEN -i tcp:$1 -t)
}
Run Code Online (Sandbox Code Playgroud)
然后source ~/.bash_profile跑
killTcpListen 8080
| 归档时间: |
|
| 查看次数: |
1413571 次 |
| 最近记录: |