How do I find processes that listens to/uses my tcp ports? I'm on mac os x.
如何找到侦听/使用tcp端口的进程?我在mac os x上。
Sometimes, after a crash or some bug, my rails app is locking port 3000. I can't find it using ps -ef... How do I find the stupid thing and kill it, brutally... ?
有时,在崩溃或bug之后,我的rails应用程序会锁定端口3000。我找不到使用ps -ef…我怎么能找到那个愚蠢的东西,然后残忍地杀死它……吗?
When doing
在做
rails server
I get
我得到
Address already in use - bind(2) (Errno::EADDRINUSE)
已使用的地址- bind(2) (Errno::EADDRINUSE)
2014 update:
2014年更新:
To complete some of the answers below: After executing the kill commands, deleting the pid file might be necessary rm ~/mypath/myrailsapp/tmp/pids/server.pid
要完成下面的一些答案:在执行kill命令之后,删除pid文件可能是必要的rm ~/mypath/myrailsapp/tmp/pids/server.pid。
15 个解决方案
#1
1555
-
You can try
netstat
你可以试试netstat
netstat -vanp tcp | grep 3000
-
For OSX El Capitan and newer (or if your netstat doesn't support
-p
), uselsof
对于OSX El Capitan和更新的(或者如果您的netstat不支持-p),可以使用lsof。
sudo lsof -i tcp:3000
#2
1051
Find:
发现:
[sudo] lsof -i :3000
Kill:
杀:
kill -9 <PID>
#3
76
Nothing above worked for me. Anyone else with my experience could try the following (worked for me):
没有比这更适合我的了。其他有我经验的人可以试试下面的方法(为我工作):
Run:
运行:
lsof -i :3000 (where 3000 is your current port in use)
then check status of the reported PID :
然后检查报告的PID状态:
ps ax | grep <PID>
finally, "begone with it":
最后,“走开”:
kill -QUIT <PID>
#4
74
You can use lsof -i:3000
.
你可以用lsof -i:3000。
That is "List Open Files". This gives you a list of the processes and which files and ports they use.
这就是“列表打开文件”。这将给您一个进程列表以及它们使用的文件和端口。
#5
61
A one-liner to extract the PID of the process using port 3000 and kill it.
使用端口3000来提取进程的PID并杀死它。
lsof -ti:3000 | xargs kill
The -t flag removes everything but the PID from the lsof output, making it easy to kill it.
-t标记除了从输出的lsof中删除PID以外的所有东西,使它很容易被杀死。
#6
44
In your .bash_profile
, create a shortcut for terminate
the 3000 process:
在您的.bash_profile中,为终止3000个进程创建一个快捷方式:
terminate(){
lsof -P | grep ':3000' | awk '{print $2}' | xargs kill -9
}
Then, call $terminate
if it's blocked.
然后,如果被阻塞,调用$terminate。
#7
31
lsof -P | grep ':3000' | awk '{print $2}'
This will give you just the pid, tested on MacOS.
这只会给你一个在MacOS上测试的pid。
#8
20
Execute in command line on OS-X El Captain:
执行在OS-X El舰长的命令行:
kill -kill `lsof -t -i tcp:3000`
Terse option of lsof returns just the PID.
简洁的lsof选项只返回PID。
#9
20
One of the ways to kill a process on a port is to use the python library: freeport (https://pypi.python.org/pypi/freeport/0.1.9) . Once installed, simply:
在端口上杀死进程的方法之一是使用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
#10
19
Find the open connection
找到打开连接
lsof -i -P | grep -i "listen"
i -P | grep -我“听”
Kill by process ID
杀的进程ID
kill -9 'PID'
kill - 9“PID”
#11
13
To forcefully kill a process like that, use the following command
要强制执行这样的过程,可以使用以下命令。
lsof -n -i4TCP:3000
Where 3000 is the port number where the process is running at
其中3000是进程正在运行的端口号?
this returns the process id(PID) and run
这将返回进程id(PID)并运行。
kill -9 "PID"
Replace PID with the number you get after running the first command
在运行第一个命令后,用得到的数字替换PID。
#12
8
Possible ways to achieve this:
可能的实现方法:
top
前
The top command is the traditional way to view your system’s resource usage and see the processes that are taking up the most system resources. Top displays a list of processes, with the ones using the most CPU at the top.
顶部命令是查看系统资源使用情况的传统方式,并查看占用最多系统资源的进程。Top显示了一个进程列表,其中使用了顶部最多的CPU。
ps
ps
The ps command lists running processes. The following command lists all processes running on your system:
ps命令列出正在运行的进程。下面的命令列出了系统上运行的所有进程:
ps -A
You could also pipe the output through grep to search for a specific process without using any other commands. The following command would search for the Firefox process:
您还可以通过grep输出输出,以搜索特定的进程,而不使用任何其他命令。以下命令将搜索Firefox进程:
ps -A | grep firefox
The most common way of passing signals to a program is with the kill command.
向程序传递信号最常见的方式是使用kill命令。
kill PID_of_target_process
lsof
lsof
List of all open files and the processes that opened them.
列出所有打开的文件和打开它们的进程。
lsof -i -P | grep -i "listen"
kill -9 PID
or
或
lsof -i tcp:3000
#13
2
Add to ~/.bash_profile
:
添加到~ / . bash_profile:
function killTcpListen () {
kill -QUIT $(sudo lsof -sTCP:LISTEN -i tcp:$1 -t)
}
Then source ~/.bash_profile
and run
然后~ /来源。bash_profile和运行
killTcpListen 8080
killTcpListen 8080
#14
0
you can use command
您可以使用命令
lsof -h
usage of this command to find port is
使用此命令来查找端口。
-i i select by IPv[46] address: [46][proto][@host|addr][:svc_list|port_list]
In your case enter
在你的情况下进入
lsof -i :3000
#15
0
You should try this code using the terminal:
您应该使用终端来尝试这段代码:
$ killall -9 ruby
#1
1555
-
You can try
netstat
你可以试试netstat
netstat -vanp tcp | grep 3000
-
For OSX El Capitan and newer (or if your netstat doesn't support
-p
), uselsof
对于OSX El Capitan和更新的(或者如果您的netstat不支持-p),可以使用lsof。
sudo lsof -i tcp:3000
#2
1051
Find:
发现:
[sudo] lsof -i :3000
Kill:
杀:
kill -9 <PID>
#3
76
Nothing above worked for me. Anyone else with my experience could try the following (worked for me):
没有比这更适合我的了。其他有我经验的人可以试试下面的方法(为我工作):
Run:
运行:
lsof -i :3000 (where 3000 is your current port in use)
then check status of the reported PID :
然后检查报告的PID状态:
ps ax | grep <PID>
finally, "begone with it":
最后,“走开”:
kill -QUIT <PID>
#4
74
You can use lsof -i:3000
.
你可以用lsof -i:3000。
That is "List Open Files". This gives you a list of the processes and which files and ports they use.
这就是“列表打开文件”。这将给您一个进程列表以及它们使用的文件和端口。
#5
61
A one-liner to extract the PID of the process using port 3000 and kill it.
使用端口3000来提取进程的PID并杀死它。
lsof -ti:3000 | xargs kill
The -t flag removes everything but the PID from the lsof output, making it easy to kill it.
-t标记除了从输出的lsof中删除PID以外的所有东西,使它很容易被杀死。
#6
44
In your .bash_profile
, create a shortcut for terminate
the 3000 process:
在您的.bash_profile中,为终止3000个进程创建一个快捷方式:
terminate(){
lsof -P | grep ':3000' | awk '{print $2}' | xargs kill -9
}
Then, call $terminate
if it's blocked.
然后,如果被阻塞,调用$terminate。
#7
31
lsof -P | grep ':3000' | awk '{print $2}'
This will give you just the pid, tested on MacOS.
这只会给你一个在MacOS上测试的pid。
#8
20
Execute in command line on OS-X El Captain:
执行在OS-X El舰长的命令行:
kill -kill `lsof -t -i tcp:3000`
Terse option of lsof returns just the PID.
简洁的lsof选项只返回PID。
#9
20
One of the ways to kill a process on a port is to use the python library: freeport (https://pypi.python.org/pypi/freeport/0.1.9) . Once installed, simply:
在端口上杀死进程的方法之一是使用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
#10
19
Find the open connection
找到打开连接
lsof -i -P | grep -i "listen"
i -P | grep -我“听”
Kill by process ID
杀的进程ID
kill -9 'PID'
kill - 9“PID”
#11
13
To forcefully kill a process like that, use the following command
要强制执行这样的过程,可以使用以下命令。
lsof -n -i4TCP:3000
Where 3000 is the port number where the process is running at
其中3000是进程正在运行的端口号?
this returns the process id(PID) and run
这将返回进程id(PID)并运行。
kill -9 "PID"
Replace PID with the number you get after running the first command
在运行第一个命令后,用得到的数字替换PID。
#12
8
Possible ways to achieve this:
可能的实现方法:
top
前
The top command is the traditional way to view your system’s resource usage and see the processes that are taking up the most system resources. Top displays a list of processes, with the ones using the most CPU at the top.
顶部命令是查看系统资源使用情况的传统方式,并查看占用最多系统资源的进程。Top显示了一个进程列表,其中使用了顶部最多的CPU。
ps
ps
The ps command lists running processes. The following command lists all processes running on your system:
ps命令列出正在运行的进程。下面的命令列出了系统上运行的所有进程:
ps -A
You could also pipe the output through grep to search for a specific process without using any other commands. The following command would search for the Firefox process:
您还可以通过grep输出输出,以搜索特定的进程,而不使用任何其他命令。以下命令将搜索Firefox进程:
ps -A | grep firefox
The most common way of passing signals to a program is with the kill command.
向程序传递信号最常见的方式是使用kill命令。
kill PID_of_target_process
lsof
lsof
List of all open files and the processes that opened them.
列出所有打开的文件和打开它们的进程。
lsof -i -P | grep -i "listen"
kill -9 PID
or
或
lsof -i tcp:3000
#13
2
Add to ~/.bash_profile
:
添加到~ / . bash_profile:
function killTcpListen () {
kill -QUIT $(sudo lsof -sTCP:LISTEN -i tcp:$1 -t)
}
Then source ~/.bash_profile
and run
然后~ /来源。bash_profile和运行
killTcpListen 8080
killTcpListen 8080
#14
0
you can use command
您可以使用命令
lsof -h
usage of this command to find port is
使用此命令来查找端口。
-i i select by IPv[46] address: [46][proto][@host|addr][:svc_list|port_list]
In your case enter
在你的情况下进入
lsof -i :3000
#15
0
You should try this code using the terminal:
您应该使用终端来尝试这段代码:
$ killall -9 ruby