In the system, the execution of programs and commands are completed through processes. By learning Linux process management, you can easily view and manage the tasks performed by the computer. Especially in a system with only command lines, work management can better facilitate managers to coordinate tasks without waiting in queues. Now, let's get to know the process management and work management of Linux. Part of the option description is the direct translation (manual) of the command help page. Please forgive me if there is an error. There is more content, and readers on the web side can use the table of contents in the left sidebar.
Process management
Process introduction
A process is a program or command being executed by the computer. Each process is an entity that runs, has its own memory address space, and consumes certain system resources. Services also function through processes.
Process management role
- View computer health status (system resource usage, etc.)
- View all processes in the system
- Force the end of the process (Kill)
Process management commands
View process (ps)
1. Use the ps
command. This command provides two formats: BSD system format and Linux standard command format. The parameters mainly introduce the Linux standard command format (both above -
)
#BSD格式查看所有进程
ps aux
#Linux标准格式查看进程
ps -[options]
#较常用的以长格式(详细信息)查看所有进程,两种都可以
ps -AL
ps -eL
ps
Command parameters ( ps --help all
see help for other specific parameters through commands):
Basic options | Description |
---|---|
-A -e | all all processes |
-a | All through any terminal device 1 process conducted |
-d | All processes except session leader 2 (not translated... to the effect that it is the main process of a session) |
-N | Means to negate (negate the condition), -Nd means to display all session leaders |
Filter options, the options must be followed by parameters, which are the specific content (ID, name, etc.) specified:
Find options | Description |
---|---|
-C | Specify the command/program name |
-G | Designated user group (real user group) |
-g | Specify session or effective group 3 (effective group) name |
-p | Specify PID (process id) process number |
-q | Specify PID (process id) process number, and use fast mode 4 |
-s | Specify the session number |
-t | Specify the terminal (tty), the default is the current terminal |
-u | Specify a valid user (user), id or username |
-U | Specify real users |
Output format options, choose what format and output content to use
Output format options | Description |
---|---|
-F | extra full (increases the occupancy information than the long format) |
-f | full (long format) |
-H | Output in a hierarchical system (process tree) |
-j | Output as work |
-l | long (long format) |
-The | Specify manual configuration format 5 |
For example, output a custom format content
ps -Ao pid,sid,ppid,c
#以下是返回结果
PID SID PPID C
2041 2016 2016 0
2289 1890 1890 0
2299 1890 2289 0
2670 1892 1892 0
Command return value
The ps command returns a list of processes whose behavior meets the query conditions (if any), listed as process-related information
Description of return value header flag:
Head logo | Description |
---|---|
PPID | (Parent Process ID) parent process ID |
PID | (Process ID) Process ID |
UID | (User ID) The user name of the spawning process |
C | CPU usage |
PRI | Dynamic process priority (dynamically changed by the system) |
NI | Process priority correction value (change by the user when needed) |
SZ | Amount of memory used |
RSS | Fixed memory occupied by the process (resident memory) |
PSR | CPU number (core number) where the process is running |
WCHAN | System resources waiting for the process |
ESTIMATES | Process start time |
TIME | Process cumulative CPU usage time |
CMD | Command name (command directory) |
- Among them, the smaller the priority number of NI and PRI options, the higher the priority. For the correction value NI, it
-20
is the highest priority correction
View process and system health status in real time (top)
Use top
commands to enter an interactive mode
The lower part of the interface is similar to the return value of the ps command, but the first few processes are displayed for dynamic sorting. See below for details. The display refresh rate is 3 seconds by default and can be parameterized.
top #不加参数使用默认刷新速率
top -d secs #指定刷新时间(以秒计)
The upper part of the interface also dynamically displays the real-time health status of the system
-
The first line of task queue information
information Description 3:17:49 up time system current time up 3:34 System running time, format dd day hh:mm 1 user A user is currently logged in load average Average load, which is the load of the first 1 minute, the first 5 minutes, and the first 15 minutes (full load is the number of CPU logical processors) -
第二行为进程信息
信息 说明 212 total 共有212个进程 2 running 有2个进程正在运行 210 sleeping 有210个进程正在睡眠(无工作未加入处理队列) 0 stopped 有0个进程已经停止 0 zombie 有0个进程僵死(僵尸进程),即进程正在关闭中(少部分情况,时间较短)或关闭时遇到阻断无法自行关闭 -
第三行为CPU占用信息
信息 说明 0.0us 用户占用0.0%CPU(指由用户发起的进程) 0.6sy 系统占用0.6%(指系统进程) 0.0 ni 优先的用户进程占用0.0% 99.4 id 空闲部分CPU 99.4% 0.0 wa 等待系统资源的进程占用0.0% 0.0 hi 硬中断请求服务占用0.0% 0.0 si 软中断请求服务占用0.0% 0.0 st 虚拟时间百分比,如果该系统下有虚拟机,虚拟机的CPU等待真实CPU处理其他虚拟机进程的时间,时间越长 -
第四行为物理内存信息
信息 说明 3912.0 total 共有3912.0MB内存(我怎么记得给了4G,估计硬件保留一点) 2455.4 free 2496.5MB 内存空闲 761.4 used 749.6MB内存已使用(Linux内存占用很小,且内存回收机制比较好) 665.2 buff/cache 内存缓冲有665.2 MB -
第五行为交换分区信息
信息 说明 3815.0 total 共有3815.0MB交换分区 3815.0 free 有3815.0MB空间剩余(全空着) 0.0 used 有0.0MB交换分区被使用 2872.8 avail mem 有2872.8MB可用等效内存(存疑)
在交互界面中可以执行的操作
以下操作可以在交互界面中执行,注意大小写区别
-
方向键键按下会立即刷新,
left
right
可以移动表格,up
down
键无用? 或 h 显示帮助 P 按CPU使用率排序(默认选项) M 按内存使用量排序 N 按PID排序 q 退出交互界面 -
其他操作在帮助中,如
Z
控制显示颜色等,交互设置界面下方均有说明内容,请自行探索
查看进程树(pstree)
使用pstree
命令可以查看运行的进程树,父子进程的关系较为直观。
pstree -p #一般使用-p选项同时显示PID
- 其中可能出现
2*
3*
等字符(不含-p
选项时),表示有同样进程名的子进程有2个/3个
管理进程(kill/killall)
从名字上看kill
命令主要用来强行杀死进程,但事实上不是这样。kill命令可以向一个任务发送一个信号,可以是重启、正常结束、强行停止、恢复运行、暂停等,信号的编号遵循SIGSPEC
信号声明 或SIGNUM
编号命名。
kill -s <信号名> PID
kill -n <信号编号> PID
#一般直接使用信号编号,而不记名称
另外,使用-l
选项可以列出信号名称
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
下面选几个常用信号介绍,
信号编号 | 信号名称 | 说明 |
---|---|---|
1 | SIGHUP | 使进程立即结束,重新初始化(读取配置文件等)然后重启 |
2 | SIGINT | 程序终止信号,用来终止前台的进程,相当于按下ctrl +c |
8 | SIGFPE | 发生致命算数运算错误时会发出 |
9 | SIGKILL | 相当于windows系统taskkill 命令的\f 参数,强制结束程序运行,该信号不能被阻塞、忽略,用于强行停止进程 |
14 | SIGALRM | 时钟定时信号,alarm函数使用这个信号 |
15 | SIGTERM | 正常结束一个进程的信号,也即非强制停止一个进程,发出信号让其正常关闭 |
18 | SIGCONT | 恢复一个已经暂停的进程,信号不能被阻断 |
19 | SIGSTOP | 暂停一个前台进程并挂入后台,相当于ctrl + z |
- 一般使用
SIGTERM
信号来结束进程,部分进程无法用这个信号结束时,使用SIGKILL
信号 - 在不包含信号参数时,
kill
命令默认使用SIGTERM
正常结束信号
killall
命令不是杀死所有进程,只是按进程名来发送信号。对于killall
命令不多说明,除了信号名称、对象不同外其他用法与kill相同(信号编号仍然相同)
killall -s <signal> <Process Name>
#注意killall命令使用的是进程名
如果要按终端号结束一个终端上登录用户的所有进程,使用pkill
命令
pkill -t <terminal name>
工作管理
在系统中支持同时进行多项工作,但只有一个终端用来显示时,使用工作管理命令将不需要交互或暂时不需要输入命令的工作放入后台运行,方便进行统筹安排,提高管理员操作效率
将工作放入后台
-
在命令/程序一开始就将其放入后台运行
在命令行后加一个
&
,直接将工作放入后台tar -zcf etc.tar.gz /etc & #任务会在后台运行,并没有任何输出,也会在后台结束
-
在命令/程序运行中将其放入后台
使用快捷键
ctrl
+z
,将工作放入后台并暂停。注意:用这个快捷键将任务放入后台后处于暂停状态,需要手动启动,启动方法在下面介绍
注意:后台运行中的工作如果有输出,会在终端中输出,不影响当前工作,但输出可以在任意位置,某些情况下可能引起看错
查看在后台中的命令及其状态
使用jobs
命令可以查看所有后台中的工作,方括号中的为工作序号(不是队列号,在工作未结束前这个序号不会变动),在后面的操作中会用到
jobs
[1]- 已停止 vim (工作目录: ~)
[2]+ 已停止 vim test1.sh
#如果这个命令在当前目录中,则不会显示,否则会显示其工作目录
jobs -l #显示工作的PID,而不是工作名称
其中:+
表示放入后台的最后一个工作,如果恢复将默认恢复它,-
表示放入后台的倒数第二个工作,其他工作没有标识
将后台的工作恢复
-
将后台的工作恢复到前台进行,使用
fg
命令fg %n #n为工作序号,在jobs命令查看后台工作处找到 #其中%也可以不加
-
将后台暂停的工作在后台恢复执行,使用
bg
命令,语法与fg
相同Note: Interactive commands cannot be run in the background . The purpose of interactive commands is to interact with the user. Without front-end interaction, the command has no meaning and will not run.
A terminal device is an interactive system opened by a user, and
TTY
is a collective term for all terminal devices; at the same time,tty
it also means a terminal that logs in locally. tty1-6 represents the command line terminal, and tty7 represents the graphical terminal. The terminal opened from the graphical interface is generally a terminal emulator, that is, a virtual terminal. Virtual terminal withpty
representation, andpts
its implementation, the virtual terminal process identity is the ps commandpts
. When the system opens a virtual terminal, it is automaticallypts/n
sorted according to the serial number ↩︎When the PID of a process is the same as the SID, this process is the session
session leader
. Generallysession leader
, there will be child processes with status identifierss
↩︎The relevant content of effective users and effective groups is introduced in my other blog , Introduction and Application of Linux Special File Permission Attributes SetUID, SetGID and Sticky BIT↩︎
In the fast mode, the command ignores all other filtering options, and the query speed is greatly accelerated. In normal mode, other filtering options are in effect at the same time, and there may be a problem that the process cannot be found or all the processes are displayed (-Ap is used at the same time, and the -A (ie -e) option has a higher priority than -p, which will overwrite the original Filter condition) ↩︎
When manually configuring the output format, the column to be output (the specific name is in the return value description below) is written in lowercase with commas separated ↩︎