在 Ubuntu 中“管理进程”通常包括:查看进程、启动/停止进程、调整优先级、监控资源、设置开机启动、以及排错。下面按实用场景给你一套清晰指南。
ps(静态查看)ps aux
ps -ef
常见字段:
PID:进程号STAT:状态(R 运行,S 睡眠,Z 僵尸)%CPU / %MEM:占用top / htop(动态查看,推荐 htop)top
sudo apt install htop
htop
k 杀进程(htop)F6 排序(CPU/内存)pstree(进程树)pstree -p
command &
kill PID
kill -9 PID # 强制
pkill nginx
killall python3
nice -n 10 command
renice -n 5 -p PID
Ubuntu 现在主要用 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
| 场景 | 推荐方式 |
|---|---|
| 一次性命令 | & / nohup |
| 长期服务 | systemd |
| 开发调试 | htop + kill |
| 自动重启 | systemd / supervisor |
示例(不挂断运行):
nohup python app.py &
ps -o ppid= -p ZOMBIE_PID
kill PARENT_PID
top
# 看谁吃 CPU / 内存
glances:综合监控supervisor:进程守护cgroup / systemd-run:资源限制htop
ps aux | grep xxx
kill PID
systemctl status xxx
journalctl -u xxx
如果你愿意,可以告诉我:
我可以直接给你对应示例。