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 - 进程快照
# 查看当前终端的进程ps# 查看系统所有进程(BSD 风格)psaux# 查看系统所有进程(标准风格)ps-ef# 查看特定用户的进程ps-unginx# 查看进程树ps-ef--forest# 查看指定 PIDps-p1234-opid,ppid,cmd# 自定义输出格式ps-eopid,ppid,user,%cpu,%mem,stat,start,time,command--sort=-%cpu|head-202.2 top - 实时进程监控
toptop 交互操作:
| 按键 | 功能 |
|---|---|
| P | 按 CPU 排序 |
| M | 按内存排序 |
| N | 按 PID 排序 |
| T | 按运行时间排序 |
| 1 | 显示所有 CPU 核心 |
| k | 杀死指定进程 |
| r | 修改进程优先级 |
| q | 退出 |
# 批处理模式,输出一次后退出top-b-n1|head-20# 只看特定用户top-unginx2.3 htop - 增强版 top(需安装)
# CentOSyuminstall-yepel-release&&yuminstall-yhtop# Ubuntuaptinstall-yhtop# 使用htop2.4 其他进程查看工具
# pgrep - 按名称查找进程 PIDpgrep nginx pgrep-uroot nginx# 指定用户# pidof - 查找程序的 PIDpidof nginx# pstree - 树形显示进程关系pstree-p# 显示 PIDpstree-ap|grepnginx三、进程控制
3.1 前台与后台
# 在后台运行命令command&# 将前台进程放到后台(暂停)Ctrl + Z# 查看后台任务jobs# 将后台任务调到前台fg%1# 调出 1 号任务# 让暂停的后台任务继续运行bg%1# 脱离终端运行(推荐)nohupcommand&nohup./script.sh>/tmp/output.log2>&1&3.2 进程优先级
Linux 使用 nice 值控制进程优先级,范围 -20(最高)到 19(最低),默认为 0。
# 以指定 nice 值启动nice-n10./backup.sh# 降低优先级nice-n-5./critical_task# 提高优先级(需要 root)# 修改运行中进程的 nice 值renice-n5-p1234# 将 PID 1234 的 nice 值改为 5renice-n-10-p1234# 提高优先级(需要 root)3.3 终止进程
# kill - 通过 PID 终止kill1234# 默认发送 SIGTERM(15),优雅终止kill-151234# 同上kill-91234# 发送 SIGKILL,强制终止(最后手段)kill-HUP1234# 发送 SIGHUP,常用于重载配置# killall - 按名称终止killallnginxkillall-9nginx# 强制终止所有 nginx 进程# pkill - 按名称或条件终止pkillnginxpkill-ualice# 终止 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 基本服务管理
# 启动/停止/重启/重载systemctl start nginx systemctl stop nginx systemctl restart nginx systemctl reload nginx# 重新加载配置(不断开连接)# 查看服务状态systemctl status nginx# 设置开机自启systemctlenablenginx# 开机自启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=enabled4.2 编写 Service 单元文件
创建/etc/systemd/system/myapp.service:
[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.targetType 说明:
| 类型 | 说明 |
|---|---|
| simple | 默认,ExecStart 启动的进程就是主进程 |
| forking | 进程会 fork 子进程,父进程退出 |
| oneshot | 执行一次性任务后退出 |
| notify | 服务就绪后发送 sd_notify 通知 |
Restart 说明:
| 值 | 说明 |
|---|---|
| no | 默认,不自动重启 |
| on-failure | 非正常退出时重启 |
| on-abnormal | 被信号终止或超时时重启 |
| always | 总是重启 |
4.3 管理自定义服务
# 创建服务文件后,重新加载 systemd 配置systemctl daemon-reload# 启动并设置开机自启systemctl start myapp systemctlenablemyapp# 查看服务日志journalctl-umyapp-f# 实时查看journalctl-umyapp--sincetoday journalctl-umyapp-n100# 最近 100 条4.4 systemd 定时器(替代 cron)
创建/etc/systemd/system/backup.timer:
[Unit] Description=Daily Backup Timer [Timer] OnCalendar=daily Persistent=true [Install] WantedBy=timers.target# 启用定时器systemctlenable--nowbackup.timer# 查看所有定时器systemctl list-timers五、资源限制
5.1 临时限制 - ulimit
# 查看当前限制ulimit-a# 查看打开文件数限制ulimit-n# 默认 1024# 临时修改ulimit-n655355.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]段中:
[Service] LimitNOFILE=65535 LimitNPROC=4096 MemoryLimit=2G CPUQuota=200%六、实战场景
场景一:排查 CPU 占用过高的进程
# 方法一:toptop-b-n1-o%CPU|head-20# 方法二:psps-eopid,user,%cpu,%mem,command--sort=-%cpu|head-10# 查看进程的线程top-H-p<PID># 查看进程在做什么strace-p<PID>-c-t# 统计系统调用场景二:清理僵尸进程
# 查找僵尸进程psaux|grep-wZ# 找到僵尸进程的父进程ps-oppid=-p<zombie_pid># 终止父进程(让 init 回收子进程)kill<parent_pid>场景三:将应用配置为系统服务
# 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 systemctlenable--nowwebapp systemctl status webapp七、小结
本篇涵盖了进程查看、控制、优先级管理、systemd 服务管理以及资源限制。掌握 systemd 服务单元文件的编写是现代运维的核心技能,能让你将任何应用管理为标准系统服务。
上一篇:03 - 用户与用户组管理
下一篇:05 - 软件包管理