Skip to main content

进程与服务

查看进程​

ps aux                  # 查看所有进程(完整格式)
ps -ef # 查看所有进程(标准格式)
ps -ef | grep nginx # 查找指定进程
top # 动态实时进程视图(按 q 退出)
htop # 更好用的 top(需安装)
pgrep -f keyword # 按名称/参数查进程 PID
pidof nginx # 按名称查进程 PID

top 常用快捷键:M 按内存排序,P 按 CPU 排序,k 杀进程,q 退出。

free -m                 # 查看内存(见系统信息篇)
uptime # 查看运行时长与负载
w # 查看登录用户与负载

前后台运行​

command &               # 后台运行
nohup command & # 后台运行,终端关闭也不中断
jobs # 查看后台任务
fg %1 # 把任务 1 调回前台
bg %1 # 把暂停的任务放到后台
Ctrl+Z # 暂停当前前台任务
Ctrl+C # 终止当前前台任务

终止进程​

kill PID                # 按 PID 终止(默认 SIGTERM)
kill -9 PID # 强制终止 ⚠️
killall nginx # 按名称终止所有匹配进程
pkill -f keyword # 按完整命令行匹配终止
信号含义
15 (TERM)优雅终止,默认
9 (KILL)强制杀掉,无法被优雅捕获

调整优先级​

nice -n 10 command      # 以较低优先级启动(10)
renice 5 -p PID # 调整已有进程优先级

systemd 服务管理​

systemctl start nginx       # 启动服务
systemctl stop nginx # 停止服务
systemctl restart nginx # 重启服务
systemctl reload nginx # 热重载配置(不中断)
systemctl status nginx # 查看状态
systemctl enable nginx # 开机自启
systemctl disable nginx # 取消开机自启
systemctl daemon-reload # 修改 unit 文件后重载
systemctl is-active nginx # 是否运行中
systemctl list-units --type=service # 列出服务
systemctl list-unit-files --type=service | grep enabled # 查看自启服务
systemctl reboot / poweroff # 重启 / 关机

SysV 老服务(兼容)​

service nginx start     # 启动
service nginx status # 状态
service --status-all # 全部状态

监听与端口​

ss -tlnp                # 查看监听端口及对应进程(推荐)
ss -tunp # 查看所有 TCP/UDP 连接
lsof -i :80 # 查看占用 80 端口的进程
fuser -k 80/tcp # 杀掉占用端口的进程

详细网络分析见 网络命令。