按程序名称或监听端口号查找进程的PID并杀死该进程

问题场景

我启动了一个应用hexo,一个开源的博客系统,该程序监听的端口号是4000
之后的启动出现了一些问题,提示端口号已经被占用:address already in use :::4000
所以我要先查找正在该程序正在运行的进程的PID,接着杀死这个进程。

已知,程序启动名称为hexo,监听端口号为4000

相关命令及用途

wyj@g40:~$ whatis ps
ps (1)               - report a snapshot of the current processes.
wyj@g40:~$ whatis lsof
lsof (8)             - list open files
wyj@g40:~$ whatis netstat
netstat (8)          - Print network connections, routing tables, interface statistics, masquerade connections, and multicast memberships
wyj@g40:~$ whatis grep
grep (1)             - print lines matching a pattern
wyj@g40:~$ whatis kill
kill (1)             - send a signal to a process
kill (2)             - send signal to a process

三种方法查找PID号

wyj@g40:~$ # 1. 按程序名称查找
wyj@g40:~$ ps -aux | grep hexo
wyj      3149  1.9  1.6 912432 62924 pts/0    Sl   09:29   0:09 hexo
wyj      3919  0.0  0.0  16172  1000 pts/1    S+   09:38   0:00 grep --color=auto hexo
wyj@g40:~$ 
wyj@g40:~$ # 2. 查看网络监听情况
wyj@g40:~$ netstat -tunlp | grep 4000
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
tcp6       0      0 :::4000                 :::*                    LISTEN      3149/hexo           
wyj@g40:~$ 
wyj@g40:~$ # 3. 列出打开的文件
wyj@g40:~$ lsof -i:4000
COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
hexo    3149 wyj   21u  IPv6  46651      0t0  TCP *:4000 (LISTEN)

三种方法都可以查出该进程对应的PID号。

杀死进程

kill -9 3149 其中3149是找出的进程PID号。

发布了270 篇原创文章 · 获赞 156 · 访问量 29万+

猜你喜欢

转载自blog.csdn.net/wuyujin1997/article/details/103790939
今日推荐