一、管理进程
1、ps命令
- 下面是man手册中的描述,注意:三种风格的参数可以混合使用,但是最好每个参数都用空格隔开,避免不必要的冲突。例如,我可以使用 ps -e l --forest来显示所有进程参数,使用UNIX风格的-e,使用BSD风格的l,以及GNU风格的–forest。
PS(1) User Commands
NAME
ps - report a snapshot of the current processes.
SYNOPSIS
ps [options]
DESCRIPTION
ps displays information about a selection of the active processes. If you want a repetitive update of the selection and the displayed information, use top(1) instead.
This version of ps accepts several kinds of options:
1 UNIX options, which may be grouped and must be preceded by a dash.
2 BSD options, which may be grouped and must not be used with a dash.
3 GNU long options, which are preceded by two dashes.
Options of different types may be freely mixed, but conflicts can appear. There are some synonymous options, which are functionally identical, due to the many standards and ps
implementations that this ps is compatible with.
-
ps的常用命令参数举例
- -e 显示所有进程
- -a 显示除控制进程和无终端进程外的所有进程
- -L 显示进程中的线程
- -l 显示UNIX风格的长列表(CMD显示命令缩写)
- l 显示BSD风格的长列表(内容更全,command显示程序实际路径,bash貌似不是)
- -U userlist 或 -g grplist 或 -p pidlist 显示对应列表中的进程
- –forest 会通过折线图的形式展示进程的层级关系
- –info 显示调试信息
- –sort order 按指定的order输出进程信息。
-
BSD风格长列表中
- F 内核分配给进程的系统标记
- TTY 进程启动时的终端设备
- TIME 运行进程需要的累计CPU时间
- STAT 代表当前进程状态的双字状态码
- 第一个参数O代表正在运行;S代表休眠;R代表可运行,正在等待;Z代表僵化
- 第二个参数:< 该进程运行在高优先级上; N 低优先级上;L该进程有页面锁定在内存中;s控制进程;l段线程的;+运行在前台。
- VSZ 进程在内存中的大小,以KB为单位
- RSS 进程在未换出时占有的物理内存
- WCHAN 进程休眠的内核函数地址。
- 下面是使用 ps l --forest展示的内容列表:
F UID PID PPID PRI NI VSZ RSS WCHAN STAT TTY TIME COMMAND
0 1000 2941 2933 20 0 19320 4960 do_wai Ss pts/0 0:00 bash
4 1000 3500 2941 20 0 20120 3176 - R+ pts/0 0:00 \_ ps l --forest
4 1000 1249 1214 20 0 173872 6040 poll_s Ssl+ tty2 0:00 /usr/lib/gdm3/gdm-x-session --run-script env GNOME_SHELL_SESSION_MODE=ubuntu /usr/bin/gnome-session --session=ubuntu
4 1000 1251 1249 20 0 590404 202160 ep_pol Rl+ tty2 1:52 \_ /usr/lib/xorg/Xorg vt2 -displayfd 3 -auth /run/user/1000/gdm/Xauthority -background none -noreset -keeptty -verbose 3
0 1000 1262 1249 20 0 509396 14916 poll_s Sl+ tty2 0:00 \_ /usr/lib/gnome-session/gnome-session-binary --session=ubuntu
0 1000 1398 1262 20 0 2670252 206032 poll_s Sl+ tty2 1:06 \_ /usr/bin/gnome-shell
0 1000 1423 1398 20 0 392528 7760 poll_s Sl tty2 0:00 | \_ ibus-daemon --xim --panel disable
0 1000 1427 1423 20 0 244372 6572 poll_s Sl tty2 0:00 | \_ /usr/lib/ibus/ibus-dconf
0 1000 1428 1423 20 0 295780 25056 poll_s Sl tty2 0:00 | \_ /usr/lib/ibus/ibus-extension-gtk3
0 1000 1715 1423 20 0 266148 16968 poll_s Sl tty2 0:00 | \_ /usr/lib/ibus/ibus-engine-libpinyin --ibus
0 1000 1865 1423 20 0 170544 6660 poll_s Sl tty2 0:00 | \_ /usr/lib/ibus/ibus-engine-simple
- UNIX 风格长列表中
+ C 进程生命周期中的CPU利用率
+ STIME 进程启动的系统时间
+ PRI 进程优先级(数字越大,优先级越低)
+ NI 谦让度值,用来参与觉得优先级
+ ADDR 进程的内存地址
+ SZ 加入进程被换出,所需交换空间的大致大小。 - 下面是使用ps -l -forest展示的内容类别
F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD
0 S 1000 2941 2933 0 80 0 - 4830 do_wai pts/0 00:00:00 bash
4 R 1000 15657 2941 0 80 0 - 4985 - pts/0 00:00:00 \_ ps
2、实时监测进程 top命令
- top命令的man页描述:
TOP(1) User Commands
NAME
top - display Linux processes
SYNOPSIS
top -hv|-bcEHiOSs1 -d secs -n max -u|U user -p pid -o fld -w [cols]
The traditional switches '-' and whitespace are optional.
DESCRIPTION
The top program provides a dynamic real-time view of a running system. It can display system summary information as well as a list of processes or threads currently being managed by the
Linux kernel. The types of system summary information shown and the types, order and size of information displayed for processes are all user configurable and that configuration can be made
persistent across restarts.
The program provides a limited interactive interface for process manipulation as well as a much more extensive interface for personal configuration -- encompassing every aspect of its oper‐
ation. And while top is referred to throughout this document, you are free to name the program anything you wish. That new name, possibly an alias, will then be reflected on top's display
and used when reading and writing a configuration file.
- top 命令的显示如下:
top - 14:04:48 up 3:15, 1 user, load average: 0.01, 0.08, 0.09
Tasks: 217 total, 1 running, 216 sleeping, 0 stopped, 0 zombie
%Cpu(s): 2.7 us, 1.0 sy, 0.0 ni, 95.7 id, 0.0 wa, 0.0 hi, 0.6 si, 0.0 st
MiB Mem : 3825.5 total, 690.7 free, 1759.4 used, 1375.4 buff/cache
MiB Swap: 2048.0 total, 2017.2 free, 30.8 used. 1101.2 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1251 anfengc+ 20 0 596668 194108 166440 S 7.9 5.0 4:52.49 Xorg
2933 anfengc+ 20 0 837576 45400 33000 S 5.6 1.2 0:15.42 gnome-terminal-
1398 anfengc+ 20 0 2671608 207820 93140 S 4.0 5.3 4:01.73 gnome-shell
2287 anfengc+ 20 0 2019300 356652 203688 S 3.2 9.1 10:22.88 Web Content
2534 anfengc+ 20 0 1877372 305232 188408 S 3.2 7.8 2:39.77 Web Content
3、结束进程kill系列命令
- kill命令
默认情况下,kill传递TERM信号给列出PID的进程,我们可以通过signal名字或是signal编号来传递对应的信号。- -l 显示可以kill可以传递的信号列表;
- -s 信号只是个请求,有时候会被忽略,使用此命令参数,强制传递信号。
KILL(1) User Commands
NAME
kill - send a signal to a process
SYNOPSIS
kill [options] <pid> [...]
DESCRIPTION
The default signal for kill is TERM. Use -l or -L to list available signals. Particularly useful signals include HUP, INT, KILL, STOP, CONT, and 0. Alternate signals may be specified in
three ways: -9, -SIGKILL or -KILL. Negative PID values may be used to choose whole process groups; see the PGID column in ps command output. A PID of -1 is special; it indicates all pro‐
cesses except the kill process itself and init.
OPTIONS
<pid> [...]
Send signal to every <pid> listed.
-<signal>
-s <signal>
--signal <signal>
Specify the signal to be sent. The signal can be specified by using name or number. The behavior of signals is explained in signal(7) manual page.
-l, --list [signal]
List signal names. This option has optional argument, which will convert signal number to signal name, or other way round.
-L, --table
List signal names in a nice table.
NOTES Your shell (command line interpreter) may have a built-in kill command. You may need to run the command described here as /bin/kill to solve the conflict.
EXAMPLES
kill -9 -1
Kill all processes you can kill.
kill -l 11
Translate number 11 into a signal name.
kill -L
List the available signal choices in a nice table.
kill 123 543 2341 3453
Send the default signal, SIGTERM, to all those processes.
+killall命令
通过名字对进程发出信号,而进程的名字支持通配符,所以使用时要格外小心。
KILLALL(1) User Commands
NAME
killall - kill processes by name
SYNOPSIS
killall [-Z, --context pattern] [-e, --exact] [-g, --process-group] [-i, --interactive] [-n, --ns PID] [-o, --older-than TIME] [-q, --quiet] [-r, --regexp] [-s, --signal SIGNAL, -SIGNAL]
[-u, --user user] [-v, --verbose] [-w, --wait] [-y, --younger-than TIME] [-I, --ignore-case] [-V, --version] [--] name ...
killall -l
killall -V, --version
DESCRIPTION
killall sends a signal to all processes running any of the specified commands. If no signal name is specified, SIGTERM is sent.
Signals can be specified either by name (e.g. -HUP or -SIGHUP) or by number (e.g. -1) or by option -s.
If the command name is not regular expression (option -r) and contains a slash (/), processes executing that particular file will be selected for killing, independent of their name.
killall returns a zero return code if at least one process has been killed for each listed command, or no commands were listed and at least one process matched the -u and -Z search criteria.
killall returns non-zero otherwise.
A killall process never kills itself (but may kill other killall processes).
- 其他类型的kill命令,还有skill、pkill等。