04 - 进程与服务管理
一、进程基础
1.1 什么是进程
进程是程序运行中的实例。每个进程拥有:
- PID:进程 ID,唯一标识
- PPID:父进程 ID
- UID/GID:运行用户和组
- 状态:运行、睡眠、僵尸等
1.2 进程状态
| 状态代码 | 状态名称 | 说明 |
|---|---|---|
| R | Running | 正在运行或在运行队列中等待 |
| S | Sleeping | 可中断睡眠,等待事件 |
| D | Disk Sleep | 不可中断睡眠(通常在等待 IO) |
| Z | Zombie | 僵尸进程,已终止但父进程未回收 |
| T | Stopped | 停止/暂停(如 Ctrl+Z) |
二、进程查看
2.1 ps - 进程快照
bash
# 查看当前终端的进程
ps
# 查看系统所有进程(BSD 风格)
ps aux
# 查看系统所有进程(标准风格)
ps -ef
# 查看特定用户的进程
ps -u nginx
# 查看进程树
ps -ef --forest
# 查看指定 PID
ps -p 1234 -o pid,ppid,cmd
# 自定义输出格式
ps -eo pid,ppid,user,%cpu,%mem,stat,start,time,command --sort=-%cpu | head -20
2.2 top - 实时进程监控
bash
top
top 交互操作:
| 按键 | 功能 |
|---|---|
| P | 按 CPU 排序 |
| M | 按内存排序 |
| N | 按 PID 排序 |
| T | 按运行时间排序 |
| 1 | 显示所有 CPU 核心 |
| k | 杀死指定进程 |
| r | 修改进程优先级 |
| q | 退出 |
bash
# 批处理模式,输出一次后退出
top -b -n 1 | head -20
# 只看特定用户
top -u nginx
2.3 htop - 增强版 top(需安装)
bash
# CentOS
yum install -y epel-release && yum install -y htop
# Ubuntu
apt install -y htop
# 使用
htop
2.4 其他进程查看工具
bash
# pgrep - 按名称查找进程 PID
pgrep nginx
pgrep -u root nginx # 指定用户
# pidof - 查找程序的 PID
pidof nginx
# pstree - 树形显示进程关系
pstree -p # 显示 PID
pstree -ap | grep nginx
三、进程控制
3.1 前台与后台
bash
# 在后台运行命令
command &
# 将前台进程放到后台(暂停)
Ctrl + Z
# 查看后台任务
jobs
# 将后台任务调到前台
fg %1 # 调出 1 号任务
# 让暂停的后台任务继续运行
bg %1
# 脱离终端运行(推荐)
nohup command &
nohup ./script.sh > /tmp/output.log 2>&1 &
3.2 进程优先级
Linux 使用 nice 值控制进程优先级,范围 -20(最高)到 19(最低),默认为 0。
bash
# 以指定 nice 值启动
nice -n 10 ./backup.sh # 降低优先级
nice -n -5 ./critical_task # 提高优先级(需要 root)
# 修改运行中进程的 nice 值
renice -n 5 -p 1234 # 将 PID 1234 的 nice 值改为 5
renice -n -10 -p 1234 # 提高优先级(需要 root)
3.3 终止进程
bash
# kill - 通过 PID 终止
kill 1234 # 默认发送 SIGTERM(15),优雅终止
kill -15 1234 # 同上
kill -9 1234 # 发送 SIGKILL,强制终止(最后手段)
kill -HUP 1234 # 发送 SIGHUP,常用于重载配置
# killall - 按名称终止
killall nginx
killall -9 nginx # 强制终止所有 nginx 进程
# pkill - 按名称或条件终止
pkill nginx
pkill -u alice # 终止 alice 的所有进程
pkill -f "python script.py" # 按完整命令行匹配
常用信号
| 信号 | 编号 | 含义 |
|---|---|---|
| SIGHUP | 1 | 挂起/重载配置 |
| SIGINT | 2 | 中断(Ctrl+C) |
| SIGTERM | 15 | 优雅终止(默认) |
| SIGKILL | 9 | 强制终止(不可捕获) |
| SIGSTOP | 19 | 暂停 |
| SIGCONT | 18 | 继续 |
最佳实践 :先
kill(SIGTERM),等待几秒,若进程仍在则kill -9。
四、systemd 服务管理
systemd 是现代 Linux(CentOS 7+/Ubuntu 16.04+)的初始化系统和服务管理器。
4.1 基本服务管理
bash
# 启动/停止/重启/重载
systemctl start nginx
systemctl stop nginx
systemctl restart nginx
systemctl reload nginx # 重新加载配置(不断开连接)
# 查看服务状态
systemctl status nginx
# 设置开机自启
systemctl enable nginx # 开机自启
systemctl disable nginx # 禁止开机自启
systemctl is-enabled nginx # 查看是否开机自启
# 查看所有服务
systemctl list-units --type=service
systemctl list-units --type=service --state=running
systemctl list-unit-files --type=service --state=enabled
4.2 编写 Service 单元文件
创建 /etc/systemd/system/myapp.service:
ini
[Unit]
Description=My Application Server
After=network.target mysql.service
Wants=mysql.service
[Service]
Type=simple
User=appuser
Group=appuser
WorkingDirectory=/opt/myapp
ExecStart=/opt/myapp/bin/start.sh
ExecStop=/opt/myapp/bin/stop.sh
ExecReload=/bin/kill -HUP $MAINPID
Restart=on-failure
RestartSec=5s
Environment=NODE_ENV=production
Environment=PORT=3000
LimitNOFILE=65535
[Install]
WantedBy=multi-user.target
Type 说明:
| 类型 | 说明 |
|---|---|
| simple | 默认,ExecStart 启动的进程就是主进程 |
| forking | 进程会 fork 子进程,父进程退出 |
| oneshot | 执行一次性任务后退出 |
| notify | 服务就绪后发送 sd_notify 通知 |
Restart 说明:
| 值 | 说明 |
|---|---|
| no | 默认,不自动重启 |
| on-failure | 非正常退出时重启 |
| on-abnormal | 被信号终止或超时时重启 |
| always | 总是重启 |
4.3 管理自定义服务
bash
# 创建服务文件后,重新加载 systemd 配置
systemctl daemon-reload
# 启动并设置开机自启
systemctl start myapp
systemctl enable myapp
# 查看服务日志
journalctl -u myapp -f # 实时查看
journalctl -u myapp --since today
journalctl -u myapp -n 100 # 最近 100 条
4.4 systemd 定时器(替代 cron)
创建 /etc/systemd/system/backup.timer:
ini
[Unit]
Description=Daily Backup Timer
[Timer]
OnCalendar=daily
Persistent=true
[Install]
WantedBy=timers.target
bash
# 启用定时器
systemctl enable --now backup.timer
# 查看所有定时器
systemctl list-timers
五、资源限制
5.1 临时限制 - ulimit
bash
# 查看当前限制
ulimit -a
# 查看打开文件数限制
ulimit -n # 默认 1024
# 临时修改
ulimit -n 65535
5.2 永久限制 - limits.conf
编辑 /etc/security/limits.conf:
# 用户 类型 资源 值
* soft nofile 65535
* hard nofile 65535
nginx soft nproc 4096
nginx hard nproc 8192
| 资源 | 说明 |
|---|---|
| nofile | 最大打开文件数 |
| nproc | 最大进程数 |
| memlock | 最大锁定内存 |
| cpu | 最大 CPU 时间(分钟) |
5.3 systemd 服务资源限制
在 service 文件的 [Service] 段中:
ini
[Service]
LimitNOFILE=65535
LimitNPROC=4096
MemoryLimit=2G
CPUQuota=200%
六、实战场景
场景一:排查 CPU 占用过高的进程
bash
# 方法一:top
top -b -n 1 -o %CPU | head -20
# 方法二:ps
ps -eo pid,user,%cpu,%mem,command --sort=-%cpu | head -10
# 查看进程的线程
top -H -p <PID>
# 查看进程在做什么
strace -p <PID> -c -t # 统计系统调用
场景二:清理僵尸进程
bash
# 查找僵尸进程
ps aux | grep -w Z
# 找到僵尸进程的父进程
ps -o ppid= -p <zombie_pid>
# 终止父进程(让 init 回收子进程)
kill <parent_pid>
场景三:将应用配置为系统服务
bash
# 1. 编写 service 文件
cat > /etc/systemd/system/webapp.service << 'EOF'
[Unit]
Description=Web Application
After=network.target
[Service]
Type=simple
User=webapp
WorkingDirectory=/opt/webapp
ExecStart=/usr/bin/node /opt/webapp/server.js
Restart=on-failure
RestartSec=3
Environment=NODE_ENV=production
[Install]
WantedBy=multi-user.target
EOF
# 2. 创建专用用户
useradd -r -s /sbin/nologin webapp
# 3. 重载并启动
systemctl daemon-reload
systemctl enable --now webapp
systemctl status webapp
七、小结
本篇涵盖了进程查看、控制、优先级管理、systemd 服务管理以及资源限制。掌握 systemd 服务单元文件的编写是现代运维的核心技能,能让你将任何应用管理为标准系统服务。
上一篇 :03 - 用户与用户组管理
下一篇 :05 - 软件包管理