【Linux运维极简教程】04-进程与服务管理 04 - 进程与服务管理一、进程基础1.1 什么是进程进程是程序运行中的实例。每个进程拥有PID进程 ID唯一标识PPID父进程 IDUID/GID运行用户和组状态运行、睡眠、僵尸等1.2 进程状态状态代码状态名称说明RRunning正在运行或在运行队列中等待SSleeping可中断睡眠等待事件DDisk Sleep不可中断睡眠通常在等待 IOZZombie僵尸进程已终止但父进程未回收TStopped停止/暂停如 CtrlZ二、进程查看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-releaseyuminstall-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# 脱离终端运行推荐nohupcommandnohup./script.sh/tmp/output.log213.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# 提高优先级需要 root3.3 终止进程# kill - 通过 PID 终止kill1234# 默认发送 SIGTERM15优雅终止kill-151234# 同上kill-91234# 发送 SIGKILL强制终止最后手段kill-HUP1234# 发送 SIGHUP常用于重载配置# killall - 按名称终止killallnginxkillall-9nginx# 强制终止所有 nginx 进程# pkill - 按名称或条件终止pkillnginxpkill-ualice# 终止 alice 的所有进程pkill-fpython script.py# 按完整命令行匹配常用信号信号编号含义SIGHUP1挂起/重载配置SIGINT2中断CtrlCSIGTERM15优雅终止默认SIGKILL9强制终止不可捕获SIGSTOP19暂停SIGCONT18继续最佳实践先killSIGTERM等待几秒若进程仍在则kill -9。四、systemd 服务管理systemd 是现代 LinuxCentOS 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--typeservice systemctl list-units--typeservice--staterunning systemctl list-unit-files--typeservice--stateenabled4.2 编写 Service 单元文件创建/etc/systemd/system/myapp.service[Unit] DescriptionMy Application Server Afternetwork.target mysql.service Wantsmysql.service [Service] Typesimple Userappuser Groupappuser WorkingDirectory/opt/myapp ExecStart/opt/myapp/bin/start.sh ExecStop/opt/myapp/bin/stop.sh ExecReload/bin/kill -HUP $MAINPID Restarton-failure RestartSec5s EnvironmentNODE_ENVproduction EnvironmentPORT3000 LimitNOFILE65535 [Install] WantedBymulti-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] DescriptionDaily Backup Timer [Timer] OnCalendardaily Persistenttrue [Install] WantedBytimers.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] LimitNOFILE65535 LimitNPROC4096 MemoryLimit2G CPUQuota200%六、实战场景场景一排查 CPU 占用过高的进程# 方法一toptop-b-n1-o%CPU|head-20# 方法二psps-eopid,user,%cpu,%mem,command--sort-%cpu|head-10# 查看进程的线程top-H-pPID# 查看进程在做什么strace-pPID-c-t# 统计系统调用场景二清理僵尸进程# 查找僵尸进程psaux|grep-wZ# 找到僵尸进程的父进程ps-oppid-pzombie_pid# 终止父进程让 init 回收子进程killparent_pid场景三将应用配置为系统服务# 1. 编写 service 文件cat/etc/systemd/system/webapp.serviceEOF [Unit] DescriptionWeb Application Afternetwork.target [Service] Typesimple Userwebapp WorkingDirectory/opt/webapp ExecStart/usr/bin/node /opt/webapp/server.js Restarton-failure RestartSec3 EnvironmentNODE_ENVproduction [Install] WantedBymulti-user.target EOF# 2. 创建专用用户useradd-r-s/sbin/nologin webapp# 3. 重载并启动systemctl daemon-reload systemctlenable--nowwebapp systemctl status webapp七、小结本篇涵盖了进程查看、控制、优先级管理、systemd 服务管理以及资源限制。掌握 systemd 服务单元文件的编写是现代运维的核心技能能让你将任何应用管理为标准系统服务。上一篇03 - 用户与用户组管理下一篇05 - 软件包管理