本文翻譯自:How to kill a process running on particular port in Linux?
I tried to close the tomcat using ./shutdown.sh
from tomcat /bin
directory. 我試圖使用tomcat /bin
目錄中的./shutdown.sh
關閉tomcat。 But found that the server was not closed properly. 但是發現服務器沒有正確關閉。 And thus I was unable to restart 因此我無法重新啓動
My tomcat is running on port 8080
. 我的tomcat在端口8080
上運行。
I want to kill the tomcat process running on 8080
. 我想殺死運行在8080
上的tomcat進程。 I first want to have the list of processes running on a specific port (8080) in order to select which process to kill. 我首先要具有在特定端口(8080)上運行的進程的列表,以便選擇要殺死的進程。
#1樓
參考:https://stackoom.com/question/mbPe/如何終止在Linux中特定端口上運行的進程
#2樓
Use the command 使用命令
sudo netstat -plten |grep java
used grep java
as tomcat
uses java
as their processes. 使用grep java
作爲tomcat
使用java
作爲其進程。
It will show the list of processes with port number and process id 它將顯示帶有端口號和進程ID的進程列表
tcp6 0 0 :::8080 :::* LISTEN
1000 30070621 16085/java
the number before /java
is a process id. /java
之前的數字是進程ID。 Now use kill
command to kill the process 現在使用kill
命令殺死進程
kill -9 16085
-9
implies the process will be killed forcefully. -9
表示該過程將被強制終止。
#3樓
This fuser 8080/tcp
will print you PID of process bound on that port. 此fuser 8080/tcp
將爲您打印該端口上綁定的進程的PID。
And this fuser -k 8080/tcp
will kill that process. 而這個fuser -k 8080/tcp
將終止該進程。
Works on Linux only. 僅適用於Linux。 More universal is use of lsof -i4
(or 6 for IPv6). 更通用的是使用lsof -i4
(對於IPv6, lsof -i4
6)。
#4樓
This prints to stdout the process ids of everything running on <port_number>
: 這將打印以輸出在<port_number>
上運行的所有進程的進程ID:
fuser -n tcp <port_number>
It also prints some stuff to stderr, so: 它還會向stderr打印一些內容,因此:
fuser -n tcp <port_number> 2> /dev/null
We can then supply these process ids to the kill
command: 然後,我們可以將這些進程ID提供給kill
命令:
sudo kill $(fuser -n tcp <port_number> 2> /dev/null)
You could also put this in a function if you do it a lot: 如果您經常這樣做,也可以將其放在函數中:
function killport() {
sudo kill $(fuser -n tcp $1 2> /dev/null)
}
#5樓
Linux : You can use this command if you know the port : Linux :如果知道端口,則可以使用此命令:
netstat -plten | grep LISTEN | grep 8080
AIX : AIX :
netstat -Aan | grep LISTEN | grep 8080
You then take the first column (example: f100050000b05bb8) and run the following command: 然後,您選擇第一列(例如:f100050000b05bb8)並運行以下命令:
rmsock f100050000b05bb8 tcpcb
kill process. 殺死進程。
#6樓
To know the pid of service running on particular port : 要知道在特定端口上運行的服務的pid:
netstat -tulnap | grep :*port_num*
you will get the description of that process. 您將獲得該過程的描述。 Now use kill or kill -9 pid. 現在使用kill或kill -9 pid。 Easily killed. 容易被殺死。
eg 例如
netstat -ap | grep :8080
tcp6 0 0 :::8080 :::* LISTEN 1880/java
Now: 現在:
kill -9 1880
Remember to run all commands as root
記住要以root
身份運行所有命令