01.Linux系统进程管理

徐亮伟, 江湖人称标杆徐。多年互联网运维工作经验,曾负责过大规模集群架构自动化运维管理工作。擅长Web集群架构与自动化运维,曾负责国内某大型电商运维工作。
个人博客"徐亮伟架构师之路"累计受益数万人。
笔者Q:552408925、572891887
架构师群:471443208

什么是进程

进程是已启动的可执行程序的运行实例,进程有以下组成部分:

分配内存, 已分配内存的地址空间
安全属性, 进程的运行身份和权限
进程代码, 运行一个或多个的线程
进程状态, 进程运行后的多种状态

静态程序, 二进制文件, 静态/bin/ls, /usr/sbin/sshd
动态进程, 程序运行的过程, 有生命周期及运行状态

进程的运行环境,包括以下几个部分:

局部和全局变量
当前的调度上下文
分配给进程使用的系统资源,例如文件描述符、网络端口等
给进程分配对应的pid,ppid

进程生命周期

程序运行时进程的状态关系:

父进程复制自己的地址空间创建新的子进程, 子进程可以继承父进程(ppid)的环境变量
每个进程都有自己的唯一ID(PID)
进程是由systemd这个父进程派生出来的子进程
子进程在运行自己的程序代码的时候, 父进程往往会进入到睡眠状态。
子进程完成程序代码发出退出信号请求,
子进程已经关闭或丢弃了其资源环境, 剩余的部分称之为僵停(僵尸Zombie)
父进程在子进程退出时收到信号会被唤醒, 清理剩余的结构,然后继续执行其自己的程序代码。

监控和管理进程

在多任务处理操作系统中,每个CPU(或核心)在一个时间点上只能处理一个进程。
在进程运行时,它对 CPU 时间和资源分配的要求会不断变化,从而为进程分配一个状态,它随着环境要求而改变。

静态监控进程

静态查看进程通常使用ps命令

了解进程如下选项:
PID,PPID
当前的进程状态
内存的分配情况
CPU 和已花费的时间
用户UID决定进程的特权

[root@xuliangwei ~]# ps aux|less
USER        PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root          1  0.0  0.3  46276  5836 ?        Ss   Feb27   0:46 /usr/lib/systemd/systemd --system --deserialize 21

USER:   //运行进程的用户
PID:    //进程 ID
%CPU:   //CPU 占用率
%MEM:   //内存占用率
VSZ:    //占用虚拟内存
RSS:    //占用实际内存 驻留内存 
TTY:    //进程运行的终端
STAT:   //进程状态 man ps (/STATE)
    R   //进程运行
    S   //可中断睡眠
    D   //不可中断睡眠
    Z   //僵尸进程
    X   //进程已经退出
    T   //进程被暂停
    Ss s        //进程的领导者,父进程
    S< <        //优先级较高的进程
    SN N        //优先级较低的进程
    R+ +        //表示是前台的进程组
    Sl          //以线程的方式运行
    START:      //进程的启动时间
    TIME:       //进程占用 CPU 的总时间
    COMMAND:    //进程文件,进程名

ps命令使用方法

//对进程的CPU进行排序展示
[root@xuliangwei ~]# ps aux --sort %cpu |less
[root@xuliangwei ~]# ps aux --sort -%cpu |less

//自定义显示字段
[root@xuliangwei ~]# ps axo user,pid,ppid,%mem,command |grep nginx
root     122914      1  0.0 nginx: master process nginx
nginx    122915 122914  0.1 nginx: worker process

//显示进程的子进程
[root@xuliangwei ~]# yum install nginx -y
[root@xuliangwei ~]# systemctl start nginx
[root@xuliangwei ~]# ps auxf |grep nginx
root     122914  0.0  0.0  46308   956 ?        Ss   17:32   0:00 nginx: master process nginx
nginx    122915  0.0  0.1  48780  1976 ?        S    17:32   0:00  \_ nginx: worker process

//查看指定进程PID
[root@xuliangwei ~]# ps aux|grep sshd
root       1157  0.0  0.1 105996  3604 ?        Ss   Feb27   0:00 /usr/sbin/sshd -D
[root@xuliangwei ~]# cat /run/sshd.pid
1157

//pgrep常用参数, -l -a
[root@xuliangwei ~]# pgrep  sshd
1157
118579
[root@xuliangwei ~]# pidof sshd
118579 1157

//查看进程树 
[root@xuliangwei ~]# pstree

动态监控进程

[root@xuliangwei ~]# top
[root@xuliangwei ~]# top -d 1
[root@xuliangwei ~]# top -d 1 -p 10126     查看指定进程的动态信息
[root@xuliangwei ~]# top -d 1 -p 10126,1 
[root@xuliangwei ~]# top -d 1 -u apache    查看指定用户的进程
[root@xuliangwei ~]# top -d 1 -b -n 2 > top.txt    将 2 次 top 信息写入到文件

top 常见指令
h   查看帮出
z 以彩色信息展示
1   显示所有CPU的负载
s   设置刷新时间
b   高亮现实处于R状态的进程
M   按内存使用百分比排序输出
P   按CPU使用百分比排序输出
R 对排序进行反转
f 自定义显示字段
k   kill掉指定PID进程
W 保存top环境设置 ~/.toprc
q   退出

系统负载的计算和意义

进程以及子进程和线程产生的计算指令都会让cpu执行,产生请求的这些进程组成"运行队列",等待cpu执行,这个队列就是系统负载, 系统负载是所有cpu的运行队列的总和.

[root@xuliangwei ~]# w
20:25:48 up 95 days,  9:06,  1 user,  load average: 2.92, 0.00, 0.00

//假设当前计算机有4个核心的cpu,当前的负载是2.92
cpu1        cpu2        cpu3        cpu4
2.94/4(个cpu核心) =  73%的cpu资源被使用,剩下27%的cpu计算资源是空想的

//假设当前的计算有2个核心的cpu,当前的负载是2.92
2.92/2 = 146%  已经验证超过了cpu的处理能力

信号管理进程

使用kill命令发送信号与进程通信
定义守护进程的角色
结束用户会话和进程
kill,killall,pgrep,pkill

[root@xuliangwei ~]# kill -l //列出所有支持的信号

//常见信号列表:
数字信号        信号别名        作用
1       HUP     挂起信号,往往可以让进程重新配置
2       INT     中断信号,起到结束进程的作用,和ctrl + c 的作用一样
3       QUIT        让进程退出,结果是进程退出
9       KILL        直接结束进程,不能被进程捕获
15      TERM        进程终止,这是默认信号
18      CONT        被暂停的进程将继续恢复运行
19      STOP        暂停进程
20      TSTP        用户停止请求,作用类似于ctrl + z 把进程放到后台并暂停

kill命令发送信号

// 给 vsftpd 进程发送信号 1,15
[root@xuliangwei ~]# yum -y install vsftpd
[root@xuliangwei ~]# systemctl start vsftpd
//发送重启信号,例如 vsftpd 的配置文件发生改变,希望重新加载
[root@xuliangwei ~]# kill -1 9160
//发送停止信号,vsftpd 服务有停止的脚本 systemctl stop vsftpd
[root@xuliangwei ~]# kill 9160


// 给vim进程发送信号 9,15
[root@xuliangwei ~]# touch file1 file2 
//使用远程终端1打开file1
[root@xuliangwei ~]# tty
/dev/pts/1
[root@xuliangwei ~]# vim file1
//使用远程终端2打开file2
[root@xuliangwei ~]# tty 
/dev/pts/2
[root@xuliangwei ~]# vim file2
//查看当前进程pid
[root@xuliangwei ~]# ps aux |grep vim
root 4362 0.0 0.2 11104 2888 pts/1 S+ 23:02 0:00 vim file1 
root 4363 0.1 0.2 11068 2948 pts/2 S+ 23:02 0:00 vim file2
//发送15信号
[root@xuliangwei ~]# kill 4362
//发送9信号
[root@xuliangwei ~]# kill -9 4363
//还可以同时给所有vim进程发送信号, 模糊匹配,同时给多个进程发送信号
[root@xuliangwei ~]# killall vim


//使用pkill踢出从远程登录到本机的用户, pkill  类似killall
[root@xuliangwei ~]# w
 20:50:17 up 95 days,  9:30,  1 user,  load average: 0.00, 0.00, 0.00
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
xuliangw pts/0    115.175.115.39   20:22    0.00s  0.01s  0.00s sshd: xuliangwei [priv]

//终止 pts/0上所有进程, 除了bash本身
[root@xuliangwei ~]# pkill -t pts/0
//终止pts/0上所有进程, 并且bash也结束(用户被强制退出)
[root@xuliangwei ~]# pkill -9 -t pts/0

//列出xuliangwei用户的所有进程,-l输出pid
[root@linux-xuliangwei ~]# pgrep -l -u xuliangwei
32206 sshd
32207 bash

后台进程管理

作业控制是一个命令行功能,允许一个 shell 实例来运行和管理多个命令。
如果没有作业控制,父进程 fork()一个子进程后,将 sleeping,直到子进程退出。
使用作业控制,可以选择性暂停,恢复,以及异步运行命令,让 shell 可以在子进程运行期间返回接受其 他命令。

前台进程,后台进程jobs,bg,fg
ctrl + Z , ctrl +c , ctrl + B

[root@xuliangwei ~]# sleep 3000 & //运行程序(时),让其在后台执行 
[root@xuliangwei ~]# sleep 4000 //^Z,将前台的程序挂起(暂停)到后台 
[2]+ Stopped sleep 4000
[root@xuliangwei ~]# ps aux |grep sleep
[root@xuliangwei ~]# jobs  //查看后台作业
[1]- Running sleep 3000 & 
[2]+ Stopped sleep 4000

[root@xuliangwei ~]# bg %2     //让作业 2 在后台运行
[root@xuliangwei ~]# fg %1     //将作业 1 调回到前台
[root@xuliangwei ~]# kill %1   //kill 1,终止 PID 为 1 的进程

[root@xuliangwei ~]# (while :; do date; sleep 2; done) & //进程在后台运行,但输出依然在当前终端
[root@xuliangwei ~]# (while :; do date; sleep 2; done) &>/dev/null &