ps: view progress
1. The function
ps command is used to list the process snapshot at the time when the ps command is executed. It is like taking a picture of the process with the collection. If you want to dynamically display the process information, you need to use the top command
2. Description of common options
Parameter options | explain |
---|---|
a | Display all processes related to the terminal, including the full path of each process |
x | Show all processes not related to the terminal |
in | Display process user information |
-e | Show all processes |
-f | Additional display of UID, PPID, C and STIME fields |
-u | Display the progress of the specified user |
-H | Show process tree |
3. Use example
1. The ps command does not accept any parameters
[root@localhost ~]# ps
PID TTY TIME CMD
9790 pts/0 00:00:00 bash
9828 pts/0 00:00:00 ps
输出的是使用者当前所在终端的进程
PID:进程的标识号
TTY:进程所属的终端控制台
TIME:进程所使用的总的CPU的时间
CMD:正在执行的命令行
2.ps common command combination (1)
ps -ef: display all processes and show the UID and PPID of each process and STIME
-e display all processes
-f additionally display UID, PPID, C and STIME bits
[root@localhost ~]# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 20:01 ? 00:00:00 /usr/lib/systemd/systemd --switched-root --system --deserialize 22
root 2 0 0 20:01 ? 00:00:00 [kthreadd]
root 3 2 0 20:01 ? 00:00:00 [ksoftirqd/0]
root 5 2 0 20:01 ? 00:00:00 [kworker/0:0H]
root 6 2 0 20:01 ? 00:00:00 [kworker/u256:0]
root 7 2 0 20:01 ? 00:00:00 [migration/0]
......
UID:进程被该UID所拥有
PID:进程的标识号
PPID:进程的父进程的标识号
C:CPU使用资源百分比
STIME:start time,进程开始的时间
TTY:该进程在哪个终端上运行,若与终端机无关,则显示“?”tty1-tty6是本机上面的登入者进程,若为pts/0等,则表示通过网络接进主机的进程
TIME:进程使用的总的CPU的时间
CMD:正在执行的命令行
3. Ps commonly used command combination (2)
ps aux display all processes and display process user information
a: display process related to the terminal
x: display process not related to the terminal
u: display user information
[root@localhost ~]# ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.0 125308 3776 ? Ss 20:01 0:00 /usr/lib/systemd/systemd --switched-root --system --deserialize 22
root 2 0.0 0.0 0 0 ? S 20:01 0:00 [kthreadd]
root 3 0.0 0.0 0 0 ? S 20:01 0:00 [ksoftirqd/0]
root 5 0.0 0.0 0 0 ? S< 20:01 0:00 [kworker/0:0H]
root 6 0.0 0.0 0 0 ? S 20:01 0:00 [kworker/u256:0]
root 7 0.0 0.0 0 0 ? S 20:01 0:00 [migration/0]
......
USER:该进程属于的用户
PID:进程的进程号
%CPU:该进程使用掉的CPU资源百分比
%MEM:该进程所占用的物理内存百分比
VSZ:该进程使用掉的虚拟内存量
RSS:该进程占用的固定的内存量
TTY:该进程在哪个终端上运行,若与终端机无关,则显示“?”tty1-tty6是本机上面的登入者进程,若为pts/0等,则表示通过网络接进主机的进程
STAT:该进程目前的状态
R:正在运行,或者是可以运行
S:正在终端睡眠中,可以由某些信号(signal)唤醒
D:不可中断睡眠
T:正在侦测或者停止了
Z:已经终止,但是其父进程无法正常终止它,从而变成zombie(僵尸)进程的状态
+:前台进程
1:多线程进程
N:低优先级进程
<:高优先级进程
s:进程领导者
START:该进程被触发启动的时间
TIME:该进程实际使用CPU运作的时间
COMMAND:该进程的实际命令
4. Display process information about the specified user
[root@localhost ~]# ps -u root
PID TTY TIME CMD
1 ? 00:00:00 systemd
2 ? 00:00:00 kthreadd
3 ? 00:00:00 ksoftirqd/0
5 ? 00:00:00 kworker/0:0H
6 ? 00:00:00 kworker/u256:0
7 ? 00:00:00 migration/0
8 ? 00:00:00 rcu_bh
5. Show process tree
ps -eH
-e: show all processes
-H: show process tree
[root@localhost ~]# ps -eH
PID TTY TIME CMD
2 ? 00:00:00 kthreadd
3 ? 00:00:00 ksoftirqd/0
5 ? 00:00:00 kworker/0:0H
6 ? 00:00:00 kworker/u256:0
7 ? 00:00:00 migration/0
8 ? 00:00:00 rcu_bh
......
9240 ? 00:00:00 sshd
9788 ? 00:00:00 sshd
9790 pts/0 00:00:00 bash
9855 pts/0 00:00:00 ps
9242 ? 00:00:00 rsyslogd
9546 ? 00:00:00 master
看sshd部分
pstree: displays the process status tree
1. The function
pstree command displays the process and the relationship between processes in a tree structure
2. Use Case
[root@localhost ~]# pstree
systemd─┬─NetworkManager───2*[{NetworkManager}]
├─VGAuthService
├─agetty
├─auditd───{auditd}
├─chronyd
├─crond
├─dbus-daemon
├─lvmetad
├─master─┬─pickup
│ └─qmgr
├─polkitd───6*[{polkitd}]
├─rsyslogd───2*[{rsyslogd}]
├─sshd───sshd───bash───pstree
├─systemd-journal
├─systemd-logind
├─systemd-udevd
├─tuned───4*[{tuned}]
└─vmtoolsd───{vmtoolsd}
pgrep: find processes matching the conditions
1. Function
pgrep can find the process number matching the condition
2. Use Case
[root@localhost ~]# pgrep ssh
9240
9788
kill: terminate the process
1. Function
kill command can terminate the process you want to stop
2. Option Description
Parameter options | explain |
---|---|
-l | List all signal names |
-p | Specify the kill command to only print the process number of the relevant process without sending any signal |
-s | Specify the signal to be sent |
3. Use case
1. List the names of all signals
[root@localhost ~]# kill -l
1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP
6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1
11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM
16) SIGSTKFLT 17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP
21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ
26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO 30) SIGPWR
31) SIGSYS 34) SIGRTMIN 35) SIGRTMIN+1 36) SIGRTMIN+2 37) SIGRTMIN+3
38) SIGRTMIN+4 39) SIGRTMIN+5 40) SIGRTMIN+6 41) SIGRTMIN+7 42) SIGRTMIN+8
43) SIGRTMIN+9 44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13
48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12
53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7
58) SIGRTMAX-6 59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2
63) SIGRTMAX-1 64) SIGRTMAX
常用信号
HUP(1):挂起,通常因为终端掉线或用户退出而引发
INT(2):中断,通常是因为按下Ctrl+c组合键来发出这个信号
QUIT(3):退出,通常是按下Ctrl+\组合键来发出这个信号
KILL(9):立即结束进程的运行
TERM(15):终止,通常在系统关机时发送
TSTP(20):暂停进程的运行,通常是按下Ctrl+z组合键来发出这个信号
2. Terminate a process The
kill command uses signal 15 by default to end the process. If the process ignores this signal, you can use signal 9 to force the process to terminate. Generally, you first check the process number to be terminated through ps and then use just kill <process number>
[root@localhost ~]# ps aux | grep docker
root 10188 1.8 2.2 586368 85356 ? Ssl 21:46 0:02 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
root 10425 0.0 1.3 301988 51108 pts/0 Sl+ 21:47 0:00 docker run nginx
kill 10188
kill -s 15 10188
kill -15 10188
以上三条命令所代表的含义都是相同的,都是给docker run nginx这个进程发送信号15来关闭
如果使用15信号无法关闭指定进程的话,可以使用kill -9 10188来进行关闭
killall: terminate process by process name
1. Function To
kill the process using the kill command, you need to obtain the pid process number of the process first, and use the killall command to directly terminate the process in the form of "killall process name".
2. Option Description
Parameter options | explain |
---|---|
-u | Terminate the specified user's process |
-w | Waiting for all terminated processes to die, killall checks once every second whether the terminated process still exists, and it only returns after they are all dead. Note that if the signal is ignored, or does not work, or the process stays in a zombie state, then killall may wait forever |
Three. Use case
1. Terminate the process name of the scheduled task
[root@localhost ~]# killall crond
[root@localhost ~]# killall crond
crond: no process found
2. Terminate all processes of the specified user
killall -u fuxiangyu nginx
终止所有归属于fuxiangyu用户的nginx进程
pkill: terminate process by process name
1. Function
pkill command can terminate the specified process by process name. Using killall to terminate a process requires contact to execute several times, and pkill can kill the specified process and all its child processes
2. Option Description
Parameter options | explain |
---|---|
-t end user | Kill the process of the specified terminal |
-u user | Kill the specified user's process |
3. Use case
1. Terminate the process by process name
pkill crond
2. Terminate the process by terminal name
使用w查看终端登录情况
[root@localhost ~]# w
22:05:55 up 2:04, 3 users, load average: 0.00, 0.01, 0.05
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 192.168.10.1 20:48 3.00s 0.11s 0.00s w
root pts/1 192.168.10.1 21:48 3:23 0.01s 0.01s -bash
fuxiangy pts/2 192.168.10.1 22:03 2:02 0.00s 0.00s -bash
我们使用通过终端名的方式来杀死进程 pts/2
此时我们使用终端pts/2打开了一个文件
[fuxiangyu@localhost ~]$ vim fuxiangyu.txt
此时在另一个终端上使用命令
[root@localhost ~]# pkill -t pts/2
回到之前的终端来查看
[fuxiangyu@localhost ~]$ vim fuxiangyu.txt
Vim: Caught deadly signal TERM
Vim: Finished.
Terminated
收到了终止进程的信号,正在执行的进程被终止了
3. Terminate the process by username
还是先查看登录终端,我们要通过用户名将fuxiangyu这个用户下的进程关闭
[root@localhost ~]# w
22:07:17 up 2:05, 3 users, load average: 0.00, 0.01, 0.05
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 192.168.10.1 20:48 5.00s 0.12s 0.00s w
root pts/1 192.168.10.1 21:48 4:45 0.01s 0.01s -bash
fuxiangy pts/2 192.168.10.1 22:03 3:24 0.00s 0.00s -bash
在终端上使用命令
[root@localhost ~]# pkill -u fuxiangyu
可以发现fuxiangyu这个终端被强制下线了!
top: Real-time display of the resource occupancy status of each process in the system
1. The function
top command is used to monitor the system processor status in real time. It can display the resource occupancy status of various processes in the system in real time. This command can sort and display the system task process according to CPU usage, memory usage and execution time. At the same time, the top command can also be set and displayed through interactive commands.
2. Option Description
Parameter options | explain |
---|---|
-a | Sort processes by used memory |
-b | Display process information in batch mode. The output can be passed to other programs or written to a file. In this mode, the top command will not accept any input and will run until the threshold set by the -n option is reached, or Press Ctrl + c and other key combination to terminate the program |
-c | Display the entire command path of the process instead of just the command name |
-d | Specify the time interval between screen refreshes |
-n | The number of times the top output information is updated, and the top command will be exited after completion |
-p | Display specified process information |
3. Usage example
1. Display process information
[root@localhost ~]# top
top - 22:21:58 up 2:20, 2 users, load average: 0.00, 0.01, 0.05
Tasks: 98 total, 1 running, 97 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.0 us, 6.2 sy, 0.0 ni, 93.8 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 3861484 total, 2754940 free, 207496 used, 899048 buff/cache
KiB Swap: 4063228 total, 4063228 free, 0 used. 3335064 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1 root 20 0 125448 3980 2608 S 0.0 0.1 0:01.25 systemd
2 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kthreadd
3 root 20 0 0 0 0 S 0.0 0.0 0:00.35 ksoftirqd/0
5 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kworker/0:0H
6 root 20 0 0 0 0 S 0.0 0.0 0:00.19 kworker/u256:0
7 root rt 0 0 0 0 S 0.0 0.0 0:00.00 migration/0
8 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcu_bh
9 root 20 0 0 0 0 S 0.0 0.0 0:00.37 rcu_sched
10 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 lru-add-drain
11 root rt 0 0 0 0 S 0.0 0.0 0:00.04 watchdog/0
13 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kdevtmpfs
14 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 netns
15 root 20 0 0 0 0 S 0.0 0.0 0:00.00 khungtaskd
16 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 writeback
第一行:
22:21:58 :当前系统时间
up 2:20 :运行了两个小时20分钟了
2 users :当前有两个用户登录系统
load average: 0.00, 0.01, 0.05:分别表示1分钟,5分钟,15分钟的平均负载情况
第二行:进程统计Tasks:
98 total:系统共有98个进程
1 running:处于运行状态的有一个
97 sleeping:一个处于休眠状态
0 stopped:stoped状态0个
0 zombie :zombie状态(僵死)的有0个
第三行:CPU状态信息 %Cpu(s)
0.0 us:用户空间占用CPU的百分比
6.2 sy:内核空间占用CPU的百分比
0.0 ni:改变过优先级的进程占用CPU的百分比
93.8 id:空闲CPU占用百分比
0.0 wa:I/O等待占用的CPU百分比
0.0 hi:硬中断占用CPU的百分比
0.0 si:软中断占用CPU的百分比
0.0 st:虚拟机占用CPU的百分比
第四行:内存状态 KiB Mem :
3861484 total:物理内存总量
2754940 free:使用中的内存总量
207496 used:空闲内存总量
899048 buff/cache:缓冲的内存量
第五行:swap交换分区信息 KiB Swap:
4063228 total:交换区总量
4063228 free:交换区空闲总量
0 used:使用的交换区总量
3335064 avail Mem:缓存的内存量
第六行:空行
第七行:给出的是各进程(任务)的状态监控
PID:进程ID
USER:进程所有者
PR:进程优先级
NI:nice值,负值表示高优先,正值表示低优先级
VIRT:进程使用的虚拟内存总量,单位为kb
RES:进程使用的,未被换出的物理内存大小,单位为kb
SHR:共享内存大小,单位为kb
S:进程状态。D=不可中断的睡眠状态,R=运行,S=睡眠 T=跟踪/停止 ,Z=僵尸进程
%CPU:上次更新到限制的CPU时间占用百分比
%MEM:进程使用的物理内存百分比
TIME+:进程使用的CPU时间总计,单位为1/100秒
COMMAND:进程名称(命令名/命令行)
2. Display the information of multi-core CPUs with different cores.
Top also supports interaction. We can press the number "1" while top is running to view the status of each CPU.
操作前
top - 22:54:33 up 1 min, 1 user, load average: 0.15, 0.12, 0.05
Tasks: 124 total, 1 running, 123 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 3861512 total, 3540468 free, 115952 used, 205092 buff/cache
KiB Swap: 4063228 total, 4063228 free, 0 used. 3500976 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1 root 20 0 190844 3812 2564 S 0.0 0.1 0:00.91 systemd
2 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kthreadd
3 root 20 0 0 0 0 S 0.0 0.0 0:00.02 ksoftirqd/0
4 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kworker/0:0
5 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kworker/0:0H
操作后
top - 22:54:55 up 2 min, 1 user, load average: 0.11, 0.11, 0.05
Tasks: 124 total, 2 running, 122 sleeping, 0 stopped, 0 zombie
%Cpu0 : 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
%Cpu1 : 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
%Cpu2 : 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
%Cpu3 : 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 3861512 total, 3540352 free, 116068 used, 205092 buff/cache
KiB Swap: 4063228 total, 4063228 free, 0 used. 3500860 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
60 root 20 0 0 0 0 S 0.6 0.0 0:00.07 kworker/3:1
18814 root 20 0 162000 2244 1580 R 0.6 0.1 0:00.01 top
1 root 20 0 190844 3812 2564 S 0.0 0.1 0:00.91 systemd
2 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kthreadd
3 root 20 0 0 0 0 S 0.0 0.0 0:00.02 ksoftirqd/0
4 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kworker/0:0
5 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kworker/0:0H
6 root 20 0 0 0 0 S 0.0 0.0 0:00.01 kworker/u256:0
7 root rt 0 0 0 0 S 0.0 0.0 0:00.02 migration/0