Linux进程管理

当程序运行在系统上时, 我们称之为 进程(process).
ps 命令可以监测 当前时刻的进程快照.
top 命令可以实时监测 进程(process).

1. ps 命令

可以通过 man ps 查看 ps 命令 的介绍.

By default, ps selects all processes with the same effective user ID (euid=EUID) as the current user and associated with the same terminal as the invoker.
It displays the process ID (pid=PID), the terminal associated with the process (tname=TTY), the cumulated CPU time in [DD-]hh:mm:ss format (time=TIME), and the executable name (ucmd=CMD). Output is unsorted by default.
ps 不带任何 参数(可选项) 时, 只会显示 当前终端下 属于 当前用户的进程:

➜  ~ ps
  PID TTY          TIME CMD
24860 pts/6    00:00:00 zsh
24892 pts/6    00:00:00 ps

PID: process ID, 进程ID, 进程的唯一标识.
TTY: teletypewriter(本意是电传打字机, 一种古老的输入设备), 这里用于标识终端.
TIME: 进程累积(cumulated)使用 CPU时间.
CMD: command, 命令.

不带任何命令行参数的 ps 看到的信息太少.
通常都是要使用命令行参数的.

ps 命令有 3 种风格的命令行参数:

  1. Unix 风格, 前面加单破折号(must be preceded by a dash)
  2. BSD 风格, 前面不加破折号(must not be used with a dash)
  3. GNU 风格, 前面加双破折号(preceded by two dashes)

ps 提供的 options 非常多, 只要记住常用的几个就够了, 其他的需要用到时查看 ps 的 manual (man ps ).

UNIX-style options, 查看所有进程:

ps -ef
# 选项解释:
# -e     Select all processes.  Identical to -A.
# -f     Do full-format listing.

BSD-style options, 查看所有进程:

ps axu

a 选项:
Lift the BSD-style “only yourself” restriction, which is imposed upon the set of all processes when some BSD-style (without “-”) options are used or when the ps personality setting is BSD-like.
The set of processes selected in this manner is in addition to the set of processes selected by other means. An alternate description is that this option causes ps to list all processes with a terminal (tty), or to list all processes when used together with the x option.
显示 所有用户终端关联的进程. 只显示有tty的进程.

x 选项:
Lift the BSD-style “must have a tty” restriction, which is imposed upon the set of all processes when some BSD-style (without “-”) options are used or when the ps personality setting is BSD-like.
The set of processes selected in this manner is in addition to the set of processes selected by other means. An alternate description is that this option causes ps to list all processes owned by you (same EUID as ps), or to list all processes when used together with the a option.
显示 当前用户所有进程. 不论进程有没有tty都显示.

u 选项:
Display user-oriented format.

ps 命令输出

➜  ~ ps -ef
UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 Jun15 ?        00:13:04 /usr/lib/systemd/systemd --system --deserialize 20
miyan    17760     1  0 Aug14 ?        00:00:00 nginx: master process nginx
...

UID: 启动进程的用户.
PID: ProcessID.
PPID: Parent ProcessID.
C: 进程生命周期中的 CPU 利用率.
STIME: 进程启动时的系统时间.
TTY: 进程启动时的终端设备.
TIME: the cumulated CPU time.
CMD: command, 启动进程的命令.

2. top 命令

ps 命令只能显示特定时间的快照, 要想实时地显示进程信息, 就需要使用 top 命令了.

top 输出的结果:
linux top
第一行显示了系统的概况:

  • 系统时间(22:49);
  • 系统已经运行了多长时间(78天);
  • 登录的用户数(1个);
  • 系统的平均负载(最近1分钟负载0, 最近5分钟负载0.02, 最近15分钟负载0.05).
    系统负载值越大说明负载越高, 由于进程短期的突发性活动, 最近 1分钟 高负载很正常, 但如果最近 15分钟 都是高负载可能就不正常了. 通常负载值超过2, 就说明系统比较繁忙了.

第二行显示了进程概要信息:
top 命令的输出中将 进程(process) 叫作 任务(task).
有多少进程处于 运行(running), 休眠(sleeping), 停止(stopped), 僵化状态(zombie, 值进程完成了, 但是父进程没有响应).

第三行显示了 CPU的概要信息.
后面2行显示了 物理内存 和 交换空间 的状态.

最后一大坨, 显示了当前运行中的进程的详细列表.

  1. PID.
  2. USER: 进程属主的名字.
  3. PR: 进程的优先级.
  4. NI: 进程的谦让度值.
  5. VIRT: 进程占用的 虚拟内存 总量.
  6. RES: 进程占用的 物理内存 总量.
  7. SHR: 进程和其他进程共享的内存总理.
  8. S: 进程的状态(D, 可中断的休眠状态; R, 运行状态; S, 休眠; T, 跟踪或停止; Z, 僵化).
  9. %CPU: 进程使用 CPU时间 比例.
  10. %MEM: 进程使用的内存占可用内存的比例
  11. TIME+: 进程启动到目前位置的 CUP时间 总量.
  12. COMMAND: 启动进程的命令.

默认情况下, top 命令 会按照 %CPU 值 对进程进行排序.
可以在 top 命令运行时使用交互命令, 每个 交互命令 都是 单字符.
键入 f , 会跳出一个新的页面, 可以选择排序字段, 也可以选择展示字段.
键入 d, 修改轮询间隔(Change-Delay-Time-interval), 默认是 3s 刷新一次.
键入 q, 退出.
更多 交互命令 参考 man top 第四部分 INTERACTIVE Commands.

3. 结束进程

kill 命令可以通过 PID 给进程发信号.
默认情况下, kill 会发 TERM 信号.

kill -l show a list of signals.

常见的Linux 进程信号有:

信号名称描述
1HUP挂起
2INT中断
3QUIT结束运行
9KILL无条件终止
11SEGV段错误
15TERM尽可能终止
17STOP无条件停止运行, 但不终止
18TSTP停止或暂停, 但继续在后台运行
19CONT在STOP或TSTP之后恢复执行

要发送进程信号, 你必须是 进程的属主 或者 root 用户.

强制结束进程:

# -信号值(注: - 是 破折号, 不是减号)
kill -9 PID
# 或者 -s 信号名称
kill -s KILL PID

kill 只能根据 PID 结束进程, 如果需要根据 进程名 结束进程, 可以使用 killall 命令, killall 支持通配符, 比如:

killall http*

参考

[1]. Linux命令行与shell脚本编程大全
[2]. 各个命令的 manual

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值