确定在某一个端口上的进程pid监听。

时间:2022-04-30 16:24:48

As the title says, I'm running multiple game servers, and every of them has the same name but different PID and the port number. I would like to match the PID of the server which is listening on certain port, and then I would like to kill this process. I need that in order to complete my bash script.

正如标题所说,我正在运行多个游戏服务器,每个服务器都有相同的名称,但是不同的PID和端口号。我想要匹配服务器的PID,它监听某个端口,然后我想要杀死这个进程。我需要它来完成我的bash脚本。

Is that even possible? Because it didn't find yet any solutions on the web.

甚至可能吗?因为它在网上找不到任何解决方案。

7 个解决方案

#1


93  

The -p flag of netstat gives you PID of the process:

netstat的-p标志为您提供了流程的PID:

netstat -l -p

Edit: The command that is needed to get PIDs of socket users in FreeBSD is sockstat. As we worked out during the discussion with @Cyclone, the line that does the job is:

编辑:需要在FreeBSD中获得套接字用户的PIDs的命令是sockstat。正如我们在与@气旋讨论时所做的一样,工作的主线是:

sockstat -4 -l | grep :80 | awk '{print $3}' | head -1

#2


112  

Short version which you can pass to kill command:

你可以通过简短的命令杀死命令:

lsof -i:80 -t

#3


8  

netstat -p -l | grep $PORT and lsof -i :$PORT solutions are good but I prefer fuser $PORT/tcp extension syntax to POSIX (which work for coreutils) as with pipe:

netstat -p -l | grep $PORT和lsof -i:$PORT解决方案很好,但是我更喜欢fuser $PORT/tcp扩展语法(用于coreutils)和pipe:

pid=`fuser $PORT/tcp`

it prints pure pid so you can drop sed magic out.

它打印的是纯粹的pid,所以你可以把sed魔法去掉。

One thing that makes fuser my lover tools is ability to send signal to that process directly (this syntax is also extension to POSIX):

使fuser成为我的情人工具的一件事是能够直接向该过程发送信号(此语法也是POSIX的扩展):

$ fuser -k $port/tcp       # with SIGKILL
$ fuser -k -15 $port/tcp   # with SIGTERM
$ fuser -k -TERM $port/tcp # with SIGTERM

Also -k is supported by FreeBSD: http://www.freebsd.org/cgi/man.cgi?query=fuser

另外,FreeBSD还支持-k: http://www.freebsd.org/cgi/man.cgi?query=fuser。

#4


7  

netstat -nlp should tell you the PID of what's listening on which port.

netstat -nlp应该告诉您什么是监听哪个端口。

#5


1  

Since sockstat wasn't natively installed on my machine I hacked up stanwise's answer to use netstat instead..

由于sockstat并不是本机安装在我的机器上的,所以我把stanwise的答案改为使用netstat。

netstat -nlp | grep -E "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\:2000" | awk '{print $7}' | sed -e "s/\/.*//g""

#6


1  

I wanted to programmatically -- using only Bash -- kill the process listening on a given port.

我想以编程的方式——只使用Bash——杀死在给定端口上侦听的进程。

Let's say the port is 8089, then here is how I did it:

假设端口是8089,这里是我怎么做的:

badPid=$(netstat --listening --program --numeric --tcp | grep "::8089" | awk '{print $7}' | awk -F/ '{print $1}' | head -1)
kill -9 $badPid

I hope this helps someone else! I know it is going to help my team.

我希望这能帮助别人!我知道这会对我的团队有帮助。

#7


0  

on windows, the netstat option to get the pid's is -o and -p selects a protocol filter, ex.: netstat -a -p tcp -o

在windows上,获得pid的netstat选项是-o和-p选择一个协议过滤器,ex.: netstat - - -p tcp -o。

#1


93  

The -p flag of netstat gives you PID of the process:

netstat的-p标志为您提供了流程的PID:

netstat -l -p

Edit: The command that is needed to get PIDs of socket users in FreeBSD is sockstat. As we worked out during the discussion with @Cyclone, the line that does the job is:

编辑:需要在FreeBSD中获得套接字用户的PIDs的命令是sockstat。正如我们在与@气旋讨论时所做的一样,工作的主线是:

sockstat -4 -l | grep :80 | awk '{print $3}' | head -1

#2


112  

Short version which you can pass to kill command:

你可以通过简短的命令杀死命令:

lsof -i:80 -t

#3


8  

netstat -p -l | grep $PORT and lsof -i :$PORT solutions are good but I prefer fuser $PORT/tcp extension syntax to POSIX (which work for coreutils) as with pipe:

netstat -p -l | grep $PORT和lsof -i:$PORT解决方案很好,但是我更喜欢fuser $PORT/tcp扩展语法(用于coreutils)和pipe:

pid=`fuser $PORT/tcp`

it prints pure pid so you can drop sed magic out.

它打印的是纯粹的pid,所以你可以把sed魔法去掉。

One thing that makes fuser my lover tools is ability to send signal to that process directly (this syntax is also extension to POSIX):

使fuser成为我的情人工具的一件事是能够直接向该过程发送信号(此语法也是POSIX的扩展):

$ fuser -k $port/tcp       # with SIGKILL
$ fuser -k -15 $port/tcp   # with SIGTERM
$ fuser -k -TERM $port/tcp # with SIGTERM

Also -k is supported by FreeBSD: http://www.freebsd.org/cgi/man.cgi?query=fuser

另外,FreeBSD还支持-k: http://www.freebsd.org/cgi/man.cgi?query=fuser。

#4


7  

netstat -nlp should tell you the PID of what's listening on which port.

netstat -nlp应该告诉您什么是监听哪个端口。

#5


1  

Since sockstat wasn't natively installed on my machine I hacked up stanwise's answer to use netstat instead..

由于sockstat并不是本机安装在我的机器上的,所以我把stanwise的答案改为使用netstat。

netstat -nlp | grep -E "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\:2000" | awk '{print $7}' | sed -e "s/\/.*//g""

#6


1  

I wanted to programmatically -- using only Bash -- kill the process listening on a given port.

我想以编程的方式——只使用Bash——杀死在给定端口上侦听的进程。

Let's say the port is 8089, then here is how I did it:

假设端口是8089,这里是我怎么做的:

badPid=$(netstat --listening --program --numeric --tcp | grep "::8089" | awk '{print $7}' | awk -F/ '{print $1}' | head -1)
kill -9 $badPid

I hope this helps someone else! I know it is going to help my team.

我希望这能帮助别人!我知道这会对我的团队有帮助。

#7


0  

on windows, the netstat option to get the pid's is -o and -p selects a protocol filter, ex.: netstat -a -p tcp -o

在windows上,获得pid的netstat选项是-o和-p选择一个协议过滤器,ex.: netstat - - -p tcp -o。