在 Ubuntu(以及其他 Linux 发行版)中,进程管理主要涉及:查看进程、控制进程(启动/暂停/结束)、优先级调整、后台运行、服务管理等。下面按常用场景系统说明。
ps(静态查看)ps aux
ps -ef
a:所有用户u:详细信息x:包括无终端进程top(动态实时)top
q 退出P 按 CPU 排序M 按内存排序htop(推荐,需安装)sudo apt install htop
htop
交互友好,支持鼠标操作。
ps aux | grep nginx
pgrep nginx
pidof nginx
./program &
或
nohup ./program &
kill PID
kill -9 PID # 强制
pkill nginx
killall nginx
范围:-20(最高)到 19(最低)
nice -n 10 command
renice -n 5 -p PID
command &
fg
fg %1
Ctrl + Z # 暂停
bg # 后台继续
jobs # 查看任务
Ubuntu 16.04+ 使用 systemd
systemctl status nginx
systemctl list-units --type=service
sudo systemctl start nginx
sudo systemctl stop nginx
sudo systemctl restart nginx
sudo systemctl enable nginx
sudo systemctl disable nginx
journalctl -u nginx
journalctl -f
top
htop
ps aux --sort=-%cpu | head
ps aux --sort=-%mem | head
sudo lsof -i:8080
sudo kill -9 PID
ulimit -n 65535
| 场景 | 命令 |
|---|---|
| 查看进程 | ps, top, htop |
| 结束进程 | kill, pkill |
| 后台运行 | &, nohup |
| 服务管理 | systemctl |
| 日志查看 | journalctl |
如果你有具体场景(如:卡死进程、服务无法启动、后台脚本管理),可以告诉我,我可以给你更针对性的命令。