Files
chill_notes/Linux/Ubuntu/Ubuntu查看进程.md
2026-04-21 20:28:53 +08:00

181 lines
2.3 KiB
Markdown
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Ubuntu 查看与管理进程
## 查看进程
### ps 命令
```bash
# 查看所有进程(标准格式)
ps -ef
# 查看所有进程BSD格式
ps aux
# 查看指定进程
ps aux | grep nginx
# 查看进程树
ps -ef --forest
# 查看进程树(更详细)
pstree
# 查看指定用户的进程
ps -u username
```
### top 命令
```bash
# 动态监控进程(按 q 退出)
top
# 高亮最高 CPU 使用率
top
# 按内存排序
# 在 top 界面按 M
# 按 CPU 排序
# 在 top 界面按 P
# 显示完整命令
top -c
# 指定用户
top -u username
```
> 💡 top 界面按键:`P` 按CPU排序、`M` 按内存排序、`k` 杀进程、`q` 退出
### htop推荐需安装
```bash
# 安装
apt install htop
# 使用
htop
```
---
## 查看端口
```bash
# 查看监听端口
netstat -tln
# 查看所有连接
netstat -an
# 查看端口占用
netstat -tlnp
# 查看指定端口
netstat -tlnp | grep 80
# 查看 TCP 端口
netstat -at
# 查看 UDP 端口
netstat -au
```
---
## 杀进程
```bash
# 正常终止进程
kill PID
# 强制终止进程
kill -9 PID
# 按名称杀进程
killall nginx
# 按名称杀进程(模糊匹配)
pkill nginx
# 查找并杀进程
ps aux | grep nginx | grep -v grep | awk '{print $2}' | xargs kill
```
---
## 进程信号
| 信号 | 值 | 说明 |
|------|-----|------|
| SIGTERM (15) | -15 | 优雅终止(默认) |
| SIGKILL (9) | -9 | 强制终止 |
| SIGSTOP (19) | -19 | 暂停进程 |
```bash
kill -l # 查看所有信号
kill -15 PID # 优雅终止
kill -9 PID # 强制终止
```
---
## 进程优先级
```bash
# 查看进程优先级
top
# 以指定优先级启动(-20 到 19越小优先级越高
nice -n 10 command
# 修改运行中进程优先级
renice 5 PID
renice -10 PID # 提高优先级需root
```
---
## 后台进程
```bash
# 后台运行
command &
# 查看后台任务
jobs
# 把暂停的进程放到后台
bg
# 把后台进程带回前台
fg
# 继续在后台运行
nohup command &
```
---
## 示例
```bash
# 1. 查看 Nginx 进程
ps aux | grep nginx
# 2. 查看端口占用
netstat -tlnp | grep :80
# 3. 杀 MySQL 进程
ps aux | grep mysql
kill -9 1234
# 4. 实时监控
top -c
```
---
> 参考:[Linux 进程管理](http://blog.sina.com.cn/s/blog_64492fe10100qqk3.html)