1. Check port occupancy
netstat -anp |grep 端口号 列出所有端口
-a或--all:显示所有连线中的Socket
-n: 显示数字地址
-p: 显示程序的PID和名称
netstat -tunlp |grep 3306 → 端口号
netstat -tunlp |grep mysql → 进程名称
netstat -tunlp |grep 29520 → 进程ID
-t: 显示 TCP 连接
-u: 显示 UDP 连接
-n: 显示数字地址
-l: 列出状态是 LISTEN 的统计信息
-p: 显示程序的PID和名称
For more information, please refer to: Linux netstat command
2. Check the process
ps [选项] 列出系统中正在运行的进程,类似windows的任务管理器
ps -A 列出所有的进程 (重要)
ps -ef 查看全格式的全部进程 (重要)
ps -w 显示加宽可以显示较多的资讯
ps -au 显示较详细的资讯
ps -aux 显示所有包含其他使用者的进程
3. Close the process
kill
The command sends a signal to the specified process or process group, causing them to act based on the signal. When no signal is specified, the default is-15
发送KILL信号来终止进程
kill -s 9 PID
kill -s SIGKILL PID
kill -s KILL PID
或者
kill -9 PID
kill -SIGKILL PID
kill -KILL PID
Common signals
kill -1 (HUP) PID 重新加载进程
kill -9 (KILL) PID 杀死一个进程(重点)
kill -15 (TERM) PID 正常停止一个进程
Demo: Close all Firefox processes
获取所有Firefox进程的ID
pidof firefox
发送KILL信号来终止所有进程号
kill -9 2551 2514 1963 1856 1771
组合为一个命令
kill -9 $(pidof firefox)
Reference: Kill command in Linux