工学1号馆

home

linux下查看监听端口对应的进程并杀死进程

Wu Yudong    October 27, 2016     Linux/Unix   1,456   

方法一:

1、通过lsof命令查看PID

wu@ubuntu:~/opt/unpv13e/tcpcliserv$ lsof -Pnl +M -i4
COMMAND    PID     USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
tcpserv01        2691     1000    3u    IPv4   19709      0t0  TCP *:9877 (LISTEN)

也可以使用lsof -Pnl +M -i4|grep 9877

wu@ubuntu:~/opt/unpv13e/tcpcliserv$  lsof -Pnl +M -i4|grep 9877
tcpserv01 2691     1000    3u  IPv4  19709      0t0  TCP *:9877 (LISTEN)

注意:i4表示ipv4,如果改为i6则为查看ipv6

本文地址:http://wuyudong.com/2016/10/27/2922.html,转载请注明出处。

2、通过ps命令查看进程情况
wu@ubuntu:~/opt/unpv13e/tcpcliserv$ ps -ef|grep 12886
wu        3237  2557  0 05:56 pts/9    00:00:00 grep –color=auto 12886

lsof命令参数解释
(1) -P :这个选项约束着网络文件的端口号到端口名称的转换。约束转换可以使lsof运行得更快一些。在端口名称的查找不能奏效时,这是很有用的。
(2) -n : 这个选项约束着网络文件的端口号到主机名称的转换。约束转换可以使lsof的运行更快一些。在主机名称的查找不能奏效时,它非常有用。
(3) -l :这个选项约束着用户ID号到登录名的转换。在登录名的查找不正确或很慢时,这个选项就很有用。
(4) +M :此选项支持本地TCP和UDP端口映射程序的注册报告。
(5) -i4 :仅列示IPv4协议下的端口。
(6) -i6 : 仅列示IPv6协议下的端口。

方法二:

1、使用netstat查看进程PID
wu@ubuntu:~/opt/unpv13e/tcpcliserv$ netstat -anp|grep 9877
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
tcp        0      0 0.0.0.0:9877            0.0.0.0:*               LISTEN      2691/tcpserv01 

2、使用ps查看进程情况
wu@ubuntu:~/opt/unpv13e/tcpcliserv$ ps -ef|grep 2691
wu        2691  2650  0 Oct26 pts/0    00:00:00 ./tcpserv01
wu        2714  2691  0 Oct26 pts/0    00:00:00 [tcpserv01] <defunct>
wu        2718  2691  0 Oct26 pts/0    00:00:00 [tcpserv01] <defunct>
wu        2723  2691  0 Oct26 pts/0    00:00:00 [tcpserv01] <defunct>
wu        2727  2691  0 Oct26 pts/0    00:00:00 [tcpserv01] <defunct>
wu        2730  2691  0 Oct26 pts/0    00:00:00 [tcpserv01] <defunct>
wu        3247  2557  0 06:02 pts/9    00:00:00 grep –color=auto 2691

kill-9 命令用于终止进程,-9 表示强迫进程立即停止,例如: kill -9 [PID]

通常用 ps 查看进程 PID ,用 kill 命令终止进程

wu@ubuntu:~/opt/unpv13e/tcpcliserv$ kill -9 2691
wu@ubuntu:~/opt/unpv13e/tcpcliserv$ netstat -anp|grep 9877
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)

说明pid=2691的进程已经杀死

如果文章对您有帮助,欢迎点击下方按钮打赏作者

Comments

No comments yet.
To verify that you are human, please fill in "七"(required)