第三十章 Linux系统进程管理详解

一、进程的概述

1.什么是进程?

言简意赅总结:当我们运行一个程序,那么我们将该程序称之为进程。

ps:
当程序运行为进程后,系统会为该进程分配内存,以及运行的身份和权限。

分配了系统的资源   文件描述符  进程描述符   网络端口   ID号    PID  PPID

系统会记录进程的运行中的状态    STATAE

在进程运行的过程中,服务器上会有各种状态来表示当前进程的指标信息。

2.进程和程序的区别

1.程序是指令和数据的有序集合    静态的概念    永久存在的

2.进程是程序在主机上面运行的一个过程   动态的概念   会随着程序的创建,运行,终止,消除而终止    临时

3.进程的生命周期

每一个进程都有其生命周期,例如创建、运行、终止和消除。这些阶段会在系统启动和运行中重复无数次。因此,进程的生命周期对于其性能的分析是非常重要的。

4.进程的运行过程

用户运行了一个程序,系统会给进程分配任务,进程会通过fork一个子进程,子进程会继承父进程的衣钵,子进程处理具体的任务,父进程就会进入等待状态,子进程任务结束,回来向父进程交任务,子进程正常退出

子进程在执行任务时,父进程因为意外原因退出了,那么子进程就会变成无人管理,就是僵尸进程

每个进程都有自己的代号  ID号  子进程  PID   父进程  PPID  

二、静态显示进程状态-ps

1.含义

ps命令来自于英文词组”process status“的缩写,其功能是用于显示当前系统的进程状态。使用ps命令可以查看到进程的所有信息,例如进程的号码、发起者、系统资源使用占比(处理器与内存)、运行状态等等。帮助我们及时的发现哪些进程出现”僵死“或”不可中断“等异常情况。

经常会与kill命令搭配使用来中断和删除不必要的服务进程,避免服务器的资源浪费。

语法格式:ps [参数]

2.常用参数

a 显示现行终端机下的所有程序,包括其他用户的程序
-A 显示所有程序
c 显示每个程序真正的指令名称,而不包含路径
-C <指令名称> 指定执行指令的名称,并列出该指令的程序的状况
-d 显示所有程序,但不包括阶段作业管理员的程序
-e 显示所有进程
e 列出程序时,显示每个程序所使用的环境变量
-f 显示UID,PPIP,C与STIME栏位
-ef 显示所有的进程及进程的PPID
f 用ASCII字符显示树状结构,表达程序间的相互关系
g 显示现行终端机下的所有程序,包括所属组的程序
-G <群组识别码> 列出属于该群组的程序的状况
h 不显示标题列
-H 显示树状结构,表示程序间的相互关系
-j 采用工作控制的格式显示程序状况
-l 采用详细的格式来显示程序状况
L 列出栏位的相关信息
-m 显示所有的执行绪
n 以数字来表示USER和WCHAN栏位
-N 显示所有的程序,除了执行ps指令终端机下的程序之外
-p <程序识别码> 指定程序识别码,并列出该程序的状况
r 只列出现行终端机正在执行中的程序
-s <阶段作业> 列出隶属该阶段作业的程序的状况
s 采用程序信号的格式显示程序状况
S 列出程序时,包括已中断的子程序资料
-t <终端机编号> 列出属于该终端机的程序的状况
-T 显示现行终端机下的所有程序
u 显示有效的用户或者uid
-U <用户识别码> 列出属于该用户的程序的状况
U <用户名称> 列出属于该用户的程序的状况
v 采用虚拟内存的格式显示程序状况
-V或V 显示版本信息
-w或w 采用宽阔的格式来显示程序状况
x 显示所有程序,不以终端机来区分
X 采用旧式的Linux i386登陆格式显示程序状况
-y 配合选项”-l”使用时,不显示F(flag)栏位,并以RSS栏位取代ADDR栏位
--cols <每列字符数> 设置每列的最大字符数
--headers 重复显示标题列
--help 在线帮助
--info 显示排错信息
--lines <显示列数> 设置显示画面的列数
aux 查看进程用户、PID、占用CPU百分比、占用内存百分比、状态、执行的命令等

3.常见示例

显示系统中全部的进程信息,含详细信息

[root@jindada ~]# ps  aux	
USER        PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root          1  0.0  0.1 125420  2112 ?        Ss   Aug03   0:03 /usr/lib/systemd/systemd --switched-root --system --des
root          2  0.0  0.0      0     0 ?        S    Aug03   0:00 [kthreadd]
root          3  0.0  0.0      0     0 ?        S    Aug03   0:00 [ksoftirqd/0]
root          5  0.0  0.0      0     0 ?        S<   Aug03   0:00 [kworker/0:0H]
root          7  0.0  0.0      0     0 ?        S    Aug03   0:00 [migration/0]
root          8  0.0  0.0      0     0 ?        S    Aug03   0:00 [rcu_bh]
root          9  0.0  0.0      0     0 ?        R    Aug03   0:07 [rcu_sched]
root         10  0.0  0.0      0     0 ?        S<   Aug03   0:00 [lru-add-drain]
root         11  0.0  0.0      0     0 ?        S    Aug03   0:00 [watchdog/0]


USER 	#进程运行的身份  用户        

PID 	#进程的ID号

PPID	#父进程的ID号

%CPU 	#进程占用CPU的百分比 

%MEM   	#进程占用内存的百分比  

VSZ  	#虚拟内存集    进程占用虚拟内存的大小  

RSS 	#物理内存集    进程占用物理内存的大小

TTY  	#终端 

	?		#没有终端   内核运行的程序 
	
	tty1	#机器的终端  服务器的本地
	
	pts/0	#虚拟远程终端  

STAT 	#进程的状态

	S		#休眠的状态 
	
	R		#正在运行的状态 
	
	D		#无法中断的休眠状态 
	
	T		#暂停或被追踪的状态
	
	Z		#僵尸状态
	
	s		#父进程 
	
	<		#优先级高
	
	N		#优先级低
	
	l		#多线程 
	
	+		#在前台运行 



START   	#进程的启动时间 

TIME 		#进程占用CPU的时间

COMMAND		#执行的指令或者命令

	[]		#内核运行的  
	
	无[]		#用户运行的  
	
	
[root@jindada ~]# ps  -ef
UID         PID   PPID  C STIME TTY          TIME CMD
root          1      0  0 Aug03 ?        00:00:03 /usr/lib/systemd/systemd --switched-root --system --deserialize 22
root          2      0  0 Aug03 ?        00:00:00 [kthreadd]
root          3      2  0 Aug03 ?        00:00:00 [ksoftirqd/0]
root          5      2  0 Aug03 ?        00:00:00 [kworker/0:0H]
root          7      2  0 Aug03 ?        00:00:00 [migration/0]
root          8      2  0 Aug03 ?        00:00:00 [rcu_bh]
root          9      2  0 Aug03 ?        00:00:07 [rcu_sched]
root         10      2  0 Aug03 ?        00:00:00 [lru-add-drain]

结合管道操作符,将当前系统运行状态中指定进程信息过滤出来

案例一
[root@jindada ~]# vim  test.log

#另外一个窗口 

[root@jindada ~]# ps  aux |grep -v grep | grep  vim
root      12592  0.2  0.2 149168  4996 pts/0    S+   09:04   0:00 vim test.log
[root@jindada ~]# ps  aux | grep  [v]im
root      12592  0.1  0.2 149168  4996 pts/0    S+   09:04   0:00 vim test.log


[root@jindada ~]# vim  test.log		#ctrl +  z  放入到后台

[1]+  Stopped                 vim test.log



[root@jindada ~]# ps  aux | grep  [v]im
root      12592  0.0  0.2 149168  4996 pts/0    T    09:04   0:00 vim test.log

案例二

[root@jindada ~]# tar  czf  etc.tar.gz   /usr  /etc/  /var  
tar: Removing leading `/' from member names
tar: Removing leading `/' from hard link targets



[root@jindada ~]# ps  aux | grep  [t]ar
root      12617  7.0  0.0 123408  1352 pts/0    R+   09:08   0:00 tar czf etc.tar.gz /usr /etc/ /var
[root@jindada ~]# ps  aux | grep  [t]ar
root      12617  6.0  0.0 123408  1352 pts/0    D+   09:08   0:00 tar czf etc.tar.gz /usr /etc/ /var
[root@jindada ~]# ps  aux | grep  [t]ar
root      12617  6.2  0.0 123540  1352 pts/0    R+   09:08   0:00 tar czf etc.tar.gz /usr /etc/ /var
[root@jindada ~]# ps  aux | grep  [t]ar
root      12617  5.8  0.0 123540  1352 pts/0    D+   09:08   0:00 tar czf etc.tar.gz /usr /etc/ /var
[root@jindada ~]# ps  aux | grep  [t]ar
root      12617  5.5  0.0 123540  1352 pts/0    D+   09:08   0:00 tar czf etc.tar.gz /usr /etc/ /var
[root@jindada ~]# ps  aux | grep  [t]ar
root      12617  6.5  0.0 123540  1352 pts/0    S+   09:08   0:00 tar czf etc.tar.gz /usr /etc/ /var
[root@jindada ~]# ps  aux | grep  [t]ar
root      12617  5.8  0.0 123540  1352 pts/0    S+   09:08   0:00 tar czf etc.tar.gz /usr /etc/ /var
[root@jindada ~]# ps  aux | grep  [t]ar
root      12617  6.1  0.0 123540  1352 pts/0    R+   09:08   0:00 tar czf etc.tar.gz /usr /etc/ /var


[root@jindada ~]# ps  aux | grep  [b]ash
root       7193  0.0  5.4 225028 111152 pts/0   Ss   Aug03   0:04 -bash
root      12575  0.0  0.1 115572  2120 pts/1    Ss+  09:04   0:00 -bash
root      12641  0.0  5.4 225028 110280 pts/0   R+   09:09   0:00 -bash


案例三

[root@jindada ~]# vim test.c 
#include <sys/types.h> 
#include <sys/wait.h> 
#include <errno.h> 
#include <unistd.h> 
#include <stdlib.h> 
#include <stdio.h> 
#include <string.h> 


int main(int argc, char *argv[]) 
{ 
pid_t pid; 
pid = fork(); 
if (pid == 0) { 
int iPid = (int)getpid(); 
fprintf(stderr,"I am child,%d\n",iPid); 
sleep(1); 
fprintf(stderr, "Child exits\n"); 
return EXIT_SUCCESS; 
} 
int iPid = (int)getpid(); 
fprintf(stderr,"I am parent,%d\n",iPid); 
fprintf(stderr, "sleep....\n"); 
sleep(600); 
fprintf(stderr, "parent exits\n"); 
return EXIT_SUCCESS; 
} 


[root@jindada ~]# gcc  test.c
[root@jindada ~]# ll
total 1218672
-rwxr-xr-x 1 root root       8696 Aug  4 09:11 a.out
-rw-r--r-- 1 root root  199327744 Aug  4 09:09 etc.tar.gz
-rw------- 1 root root 1048576000 Aug  3 11:22 swap.txt
-rw-r--r-- 1 root root        547 Aug  4 09:10 test.c
[root@jindada ~]# ./a.out 
I am parent,12655
sleep....
I am child,12656
Child exits
^C



[root@jindada ~]# ps  aux | grep  [a.]out
root      12655  0.0  0.0   4208   352 pts/0    S+   09:11   0:00 ./a.out
root      12656  0.0  0.0      0     0 pts/0    Z+   09:11   0:00 [a.out] <defunct>

结合管道操作符,将当前系统运行状态中指定用户的进程信息过滤出来

[root@jindada ~]# ps -u root
   PID TTY          TIME CMD
     1 ?        00:00:01 systemd
     2 ?        00:00:00 kthreadd
     3 ?        00:00:00 rcu_gp
     4 ?        00:00:00 rcu_par_gp
………………省略部分输出信息………………

结合管道操作符与sort命令,依据处理器使用量(第三列)情况降序排序

[root@jindada ~]# ps aux | sort -rnk 3
USER        PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root       2341  0.4  8.1 4504040 164896 tty2   Sl+  20:05   0:24 /usr/bin/gnome-shell
root       4534  0.3  0.4 220064  8520 ?        Ssl  21:37   0:00 /usr/libexec/nm-dispatcher
gdm        1541  0.1  7.3 4211428 147400 tty1   Sl+  20:05   0:06 /usr/bin/gnome-shell

………………省略部分输出信息………………

结合管道操作符与sort命令,依据内存使用量(第四列)情况降序排序

[root@jindada ~]# ps aux | sort -rnk 4
USER        PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root       2341  0.4  8.1 4503976 164828 tty2   Sl+  20:05   0:27 /usr/bin/gnome-shell
gdm        1541  0.1  7.3 4211428 147556 tty1   Sl+  20:05   0:08 /usr/bin/gnome-shell
root       2661  0.0  3.1 1271636 63004 tty2    Sl+  20:05   0:01 /usr/bin/gnome-software --gapplication-service
………………省略部分输出信息………………

三、动态显示进程状态-top

1.含义

top命令的功能是用于实时显示系统运行状态,包含处理器、内存、服务、进程等重要资源信息。运维工程师们常常会把top命令比作是“加强版的Windows任务管理器”,因为除了能看到常规的服务进程信息以外,还能够对处理器和内存的负载情况一目了然,实时感知系统全局的运行状态,非常适合作为接手服务器后执行的第一条命令。

语法格式:top [参数]

2.常用参数

-d <秒> 改变显示的更新速度
-c 切换显示模式
-s 安全模式,不允许交互式指令
-i 不显示任何闲置或僵死的行程
-n 设定显示的总次数,完成后将会自动退出
-b 批处理模式,不进行交互式显示

3.以默认格式显示系统运行信息各行含义

3.1.示例信息

[root@jindada ~]# top 
top - 09:33:58 up 23:31,  2 users,  load average: 0.24, 0.10, 0.07
Tasks: 113 total,   1 running, 112 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 :  2028088 total,   155648 free,   363844 used,  1508596 buff/cache
KiB Swap:  2097148 total,  2069756 free,    27392 used.   974036 avail Mem 

   PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND

3.2 第一行表示含义

# 第一行表示含义:
top - 09:33:58 up 23:31,  2 users,  load average: 0.24, 0.10, 0.07

09:33:58	#当前系统时间

up 23:31	#系统运行时间 

2 users		#登录用户的数量  连接终端的数量 

load average: 0.24, 0.10, 0.07			#系统平均负载时间   1  5  15 分钟的平均负载 

3.3 第二行表示含义

# 第二行表示含义:
Tasks: 113 total,   1 running, 112 sleeping,   0 stopped,   0 zombie

任务:

113 total,		#总共有113个任务 

1 running		#一个在运行状态

112 sleeping	#112休眠状态  S  

0 stopped		#0个停止状态

0 zombie		#0个在僵尸状态

3.4 第三行表示含义

%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 
 
系统各个状态所占用CPU的时间百分比  	

0.0 us		#用户态  用户所占用的CPU的时间百分比

0.0 sy		#系统态  系统所占用的CPU的时间百分比

0.0 ni		#使用优先级的程序所占用的CPU的时间百分比

100.0 id	#空闲状态所占用的CPU的时间百分比

0.0 wa		#IO等待所占用的CPU的时间百分比

0.0 hi		#硬中断所占用的CPU的时间百分比

0.0 si		#软中断所占用的CPU的时间百分比

0.0 st		#宿主机的监控程序所窃取的虚拟机的CPU的时间百分比 

3.5 第四行表示含义

KiB Mem :  2028088 total,   155648 free,   363844 used,  1508596 buff/cache


物理内存     总的 				剩余的        已使用的       缓存和缓冲中的内存 

3.6 第五行表示含义

KiB Swap:  2097148 total,  2069756 free,    27392 used.   974036 avail Mem 

虚拟内存:
PID 		#进程的ID号

USER		#运行的身份  用户       

PR  		#优先级  20

NI  		#nice值    跟PR 相差 20个数    值越低,优先级就越高 

VIRT    	#所占用的虚拟内存的大小

RES  		#所占用的物理内存的大小  

SHR 		#共享内存 

S 			#进程的状态 

%CPU 		#占用CPU的百分比

%MEM 		#占用内存的百分比      

TIME+ 		#CPU的运行时间

COMMAND		#运行的指令或者命令 

4.top的内部指令

h     #查看帮出

z     #高亮显示

1     #显示所有CPU的负载

s     #设置刷新时间

b     #高亮显示处于R状态的进程

M     #按内存使用百分比排序输出

P     #按CPU使用百分比排序输出

R     #对排序进行反转

f     #自定义显示字段

k     #kill掉指定PID进程

W     #保存top环境设置 ~/.toprc 

q     #退出

5.常见示例

以默认格式显示系统运行信息,但提供完整的进程路径及名称

[root@jindada ~]# top -c    

以批处理模式显示程序信息

[root@jindada ~]# top -b

设定每隔5秒刷新一次信息

[root@jindada ~]# top -d 5

查看某个进程ID的进程状态

[root@jindada ~]# top  -d  1  -p 1

显示指定运行用户的进程状态

[root@jindada ~]# top  -d  1  -u  postfix

设定总显示次数为3回,随后自动退出命令

[root@jindada ~]# top -n 5

四、互动的进程查看器-htop

1.含义

htop是linux系统中的一个互动的进程查看器,一个文本模式的应用程序(在控制台或者X终端中),需要ncurses。htop比较人性化。它可让用户交互式操作,支持颜色主题,可横向或纵向滚动浏览进程列表,并支持鼠标操作。

htop相比较top的优势有可以横向或纵向滚动浏览进程列表,以便看到所有的进程和完整的命令行、在启动上比top更快、杀进程时不需要输入进程号、支持鼠标选中操作、top已不再维护。

语法格式:htop [参数]

2.安装

[root@jindada ~]# yum install  -y  htop

3.常用参数

-C 使用单色配色方案
-d 设置更新的延迟, 单位是10微秒
-s 纵列排序
-u 只显示一个指定用户的进程
-p 只显示给用户
-h 打印此命令帮助
-v 打印版本信息

4.交互式命令

space 标记一个进程
U 取消所有标记
l 显示进程打来的文件
u 显示特定用户
M 按照内存排序
P 按照CPU排序
T 按在线时长排序
直接输入数字 查找对应的PID进程

5.常见示例

启动htop

[root@jindada ~]# htop

以单色模式启动htop

[root@jindada ~]# htop -C

设置显示更新的延迟为3秒

[root@jindada ~]# htop -d 3

显示一个给定的用户的进程

[root@jindada ~]# htop -u jhno1

五、高层次的多子系统概览-glances

1.含义

Linux系统自带了很多系统性能监控工具,如top,vmstat,iftop等等,还有一款监视工具glances,它能把其他几个监控的指标都集于一身。Glances是一个相对比较新的系统监控工具,用 Python 编写的,使用 psutil 库从系统获取信息。可以用它来监控 CPU、平均负载、内存、网络接口、磁盘 I/O,文件系统空间利用率、挂载的设备、所有活动进程以及消耗资源最多的进程。Glances 有很多有趣的选项。它的主要特性之一是可以在配置文件中设置阀值(careful小心、warning警告、critical致命),然后它会用不同颜色显示信息以表明系统的瓶颈。

语法格式:glances [参数]

2.安装

[root@jindada ~]# yum install  -y  glances

3.常用参数

-b 显示网络连接速度 Byte/ 秒
-s 设置 glances 运行模式为服务器
-B 绑定服务器端 IP 地址或者主机名称
-c 连接 glances 服务器端
-t 设置屏幕刷新的时间间隔,单位为秒,默认值为 2 秒,数值许可范围:1~32767

4.常见示例

172.17.0.15 主机启动 glances 服务

[root@jindada ~]# glances -s -B 172.17.0.15 &
[1] 20799
[root@jindada ~]# Glances server is running on 172.17.0.15:61209

从另一台主机查看 172.17.0.15 主机的系统负载状态

[root@jindada ~]# glances -c 172.17.0.15

显示网络连接速度 Byte/ 秒

[root@jindada ~]# glances -b

设置屏幕刷新的时间间隔为6秒

[root@jindada ~]# glances -t 6

六、终止进程-kill

1.含义

kill命令的功能是用于杀死(结束)进程,与英文单词的含义相同。Linux系统中如需结束某个进程,既可以使用如service或systemctl的管理命令来结束服务,也可以使用kill命令直接结束进程信息。

如使用kill命令后进程并没有被结束,则可以使用信号9进行强制杀死动作。

语法格式:kill [参数] 进程号

2.常用参数

-l 列出系统支持的信号
-s 指定向进程发送的信号
-a 不限制命令名和进程号的对应关系
-p 不发送任何信号

3.常见示例

列出当前系统所支持的信号

[root@jindada ~]# 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		#重新加载配置   进程pid的不变  

2		#终止信号  ctrl + c

9		#终止进程   强制终止 

15		#默认信号  终止进程   可加可不加 

18		#让暂停的进程继续运行

20		#ctrl + z  将进程暂停

重新加载配置 ,进程pid的不变

[root@jindada ~]# yum install  -y  nginx 

[root@jindada ~]# systemctl  start  nginx
[root@jindada ~]# ps aux | grep nginx
root      13063  0.0  0.1 120796  2096 ?        Ss   10:36   0:00 nginx: master process /usr/sbin/nginx
nginx     13064  0.2  0.1 121180  3128 ?        S    10:36   0:00 nginx: worker process


[root@jindada ~]# kill  -1  13063
[root@jindada ~]# ps aux | grep nginx
root      13063  0.0  0.2 121452  5248 ?        Ss   10:36   0:00 nginx: master process /usr/sbin/nginx
nginx     13070  0.0  0.1 121876  3836 ?        S    10:37   0:00 nginx: worker process



[root@jindada ~]# netstat  -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      13063/nginx: master

[root@jindada ~]# kill -1  13063
[root@jindada ~]# netstat  -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:81              0.0.0.0:*               LISTEN      13063/nginx: master

强制结束某个指定的进程(数字为对应的PID值)

[root@jindada ~]# top

[root@jindada ~]# ps  aux |grep  [t]op
root      13115  0.0  0.1 161988  2212 pts/1    S+   10:41   0:00 top
[root@jindada ~]# kill -2 13115
[root@jindada ~]# ps  aux |grep  [t]op

[root@jindada ~]# kill  -9  13063

结束某个指定的进程(数字为对应的PID值)

[root@jindada ~]# kill 13141
[root@jindada ~]# ps  aux |grep  [t]op
root      13144  0.0  0.1 161984  2212 pts/1    S+   10:44   0:00 top
[root@jindada ~]# kill -15 13144

七、使用进程名称来杀死一组进程-killall

1.含义

killall命令使用进程的名称来杀死进程,使用此指令可以杀死一组同名进程。我们可以使用kill命令杀死指定进程PID的进程,如果要找到我们需要杀死的进程,我们还需要在之前使用ps等命令再配合grep来查找进程,而killall把这两个过程合二为一,是一个很好用的命令。

我们可以使用kill命令杀死指定进程PID的进程,如果要找到我们需要杀死的进程,我们还需要在之前使用ps等命令再配合grep来查找进程,而killall把这两个过程合二为一,是一个很好用的命令。

语法格式:killall [参数]

2.常用参数

-e 对长名称进行精确匹配
-l 打印所有已知信号列表
-p 杀死进程所属的进程组
-i 交互式杀死进程,杀死进程前需要进行确认
-r 使用正规表达式匹配要杀死的进程名称
-s 用指定的进程号代替默认信号“SIGTERM”
-u 杀死指定用户的进程

3.常见示例

杀死httpd所有进程

[root@jindada ~]# killall -9 httpd

使用-l参数打印所有已知信号列表

[root@jindada ~]# killall -l

根据名称终止进程,精确匹配

kill		#根据进程ID去终止进程的   进程不存在时,会进行提示  
[root@jindada ~]# killall   top

[root@jindada ~]# killall  nginx

[root@jindada ~]# killall  nginx		#进程不存在时,会提示 
nginx: no process found

[root@jindada ~]# ps  aux | grep  [v]im
root      13264  0.0  0.2 149168  4972 pts/0    S+   10:53   0:00 vim 123.txt
root      13265  0.1  0.2 149168  4972 pts/1    S+   10:53   0:00 vim 123.log
[root@jindada ~]# killall  vim

八、依据进程名杀死指定进程-pkill

1.含义

pkill命令来自于英文词组“process kill”的缩写,其功能是依据进程名杀死指定进程。pkill命令与kill、killall十分相似,都是用于杀死(结束)指定进程的命令。

*获取进程名称可使用ps命令,获取进程号码可使用pidof命令。

语法格式:pkill [参数] 进程名

2.常用参数

-P 指定父进程号
-g 指定进程组
-t 指定开启进程的终端

3.常见示例

杀死指定名称的进程

pkill		#根据进程名称终止   终止进程不会提示    模糊杀手   


[root@jindada ~]# ps  aux | grep  [t]op
root      13278  0.0  0.1 161988  2216 pts/0    S+   10:56   0:00 top
root      13279  0.1  0.1 122396  2112 pts/1    S+   10:56   0:00 htop
[root@jindada ~]# pkill   top
[root@jindada ~]# ps  aux | grep  [t]op
[root@jindada ~]# 

[root@jindada ~]# ps  aux |grep  sh
root       6935  0.0  0.0 112756  1276 ?        Ss   Aug03   0:00 /usr/sbin/sshd -D
root       7191  0.0  0.0 161364  1616 ?        Ss   Aug03   0:00 sshd: root@pts/0
root       7193  0.0  5.4 225028 111168 pts/0   Ss+  Aug03   0:05 -bash
root      12573  0.0  0.2 161364  5936 ?        Ss   09:04   0:00 sshd: root@pts/1
root      12575  0.0  0.1 115572  2196 pts/1    Ss   09:04   0:00 -bash
root      13215  0.0  0.2 161364  6056 ?        Ss   10:50   0:00 sshd: root@pts/2
root      13217  0.0  0.1 115572  2184 pts/2    Ss   10:50   0:00 -bash
root      13296  0.0  0.0 113176  1196 pts/2    S+   10:59   0:00 sh 123.txt
root      13299  0.0  0.0 113176  1216 ?        Ss   10:59   0:00 /bin/sh -c /usr/sbin/ntpdate  ntp.aliyun.com &>/dev/null
root      13302  0.0  0.0 112708   976 pts/1    R+   10:59   0:00 grep --color=auto sh
[root@jindada ~]# pkill  sh  

九、 以树状图显示进程-pstree

1.含义

Linux系统中pstree命令的英文全称是“process tree”,即将所有行程以树状图显示,树状图将会以 pid (如果有指定) 或是以 init 这个基本行程为根 (root),如果有指定使用者 id,则树状图会只显示该使用者所拥有的行程。

语法格式: pstree [参数]

2.常用参数

-a 显示每个程序的完整指令,包含路径,参数或是常驻服务的标示
-c 不使用精简标示法
-G 使用VT100终端机的列绘图字符
-h 列出树状图时,特别标明现在执行的程序

3.常见示例

显示当前所有进程的进程号和进程id

[root@jindada ~]# pstree -p

显示所有进程的所有详细信息,遇到相同的进程名可以压缩显示

[root@jindada ~]# pstree  -a

以树形结构显示进程状态

[root@jindada ~]# pstree
systemd─┬─NetworkManager───2*[{NetworkManager}]
        ├─VGAuthService
        ├─auditd───{auditd}
        ├─crond───crond───sh───ntpdate
        ├─dbus-daemon
        ├─firewalld───{firewalld}
        ├─login───bash
        ├─master─┬─pickup
        │        └─qmgr
        ├─ping
        ├─polkitd───6*[{polkitd}]
        ├─rsyslogd───2*[{rsyslogd}]
        ├─sshd───sshd───bash───pstree
        ├─systemd-journal
        ├─systemd-logind
        ├─systemd-udevd
        ├─tuned───4*[{tuned}]
        ├─vmtoolsd
        └─vsftpd

十、管理后台的进程-screen

1.什么是后台进程

通常进程都会在终端前台运行,但是一旦关闭终端,进程也会随着结束,那么此时我们就希望进程能在后台运行,就是将在前台运行的进程放到后台运行,这样即使我们关闭了终端也不影响进程的正常运行。

2. 为什么要把进程放到后台运行

企业中很多时候会有一些需求:

比如:我们此前在国内服务器往国外服务器传输大文件时,由于网络问题需要传输很久,如果在传输的过程中出现网络抖动或者不小心关闭了终端则会导致传输失败,如果能将传输的进程放入后台,是不是就能解决此类问题了。

3. 使用什么工具可以把进程放到后台

 早期的时候,大家都选择使用&符号将进程放到后台运行,然后再使用jobs、bg、fg等方式查看进程状态,但太麻烦了,也不直观,所以我们推荐使用screen。

4.jobs、bg、fg的使用(不推荐,了解即可)

[root@jindada ~]# sleep 3000 &         #运行程序(时),让其在后台执行

[root@jindada ~]# sleep 4000         #^Z,将前台的程序挂起(暂停)到后台

[2]+ Stopped sleep 4000
[root@jindada ~]# ps aux |grep sleep
[root@jindada ~]# jobs                 #查看后台作业
[1]- Running sleep 3000 & 
[2]+ Stopped sleep 4000


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


#进程在后台运行,但输出依然在当前终端


[root@jindada ~]# (while :; do date; sleep 2; done) &

[root@jindada ~]# (while :; do date; sleep 2; done) &>/dev/null &

5.screen的使用(强烈推荐,生产必用)

5.1 含义

screen命令为多重视窗管理程序。此处所谓的视窗,是指一个全屏幕的文字模式画面。通常中有在使用telnet登入主机或是使用老式的终端机时,才有可能用到screen程序。

语法格式:screen [参数]

5.2 安装

[root@jindada ~]# yum install screen -y

5.3 常用参数

-A 将所有的视窗都调整为目前终端机的大小
-d<作业名称> 将指定的screen作业离线
-m 即使目前已在作业中的screen作业,仍强制建立新的screen作业
-r<作业名称> 恢复离线的screen作业
-R 先试图恢复离线的作业,若找不到离线的作业,即建立新的screen作业
-s 指定建立新视窗时,所要执行的shell
-S<作业名称> 指定screen作业的名称
-v 显示版本信息
-x 恢复之前离线的screen作业
-ls 显示目前所有的screen作业
-list 显示目前所有的screen作业
-wipe 检查目前所有的screen作业,并删除已经无法使用的screen作业

5.4 常见示例

创建screen终端

[root@jindada ~]# screen

开启一个screen窗口,指定名称

#1.开启一个screen窗口,指定名称
[root@jindada ~]# screen -S wget_mysql

#2.在screen窗口中执行任务即可。

#3.平滑的退出screen,但不会终止screen中的任务。注意: 如果使用exit才算真的关闭screen窗口
ctrl+a+d

查看当前正在运行的screen有哪些

[root@jindada ~]# screen -list
There is a screen on:
    22058.wget_mysql    (Detached)
1 Socket in /var/run/screen/S-root.

进入正在运行的screen

[root@jindada ~]# screen -r wget_mysql
[root@jindada ~]# screen -r 22058

[root@jindada ~]# tar  czf  etc.tar.gz     /usr/  /etc/  /var  
tar: Removing leading `/' from member names
tar: Removing leading `/' from hard link targets


Connection closed.

Disconnected from remote host(虚拟机-10.0.0.100) at 11:13:33.

Type `help' to learn how to use Xshell prompt.
[C:\~]$ 

Connecting to 10.0.0.100:22...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.

Last login: Tue Aug  4 11:12:44 2020 from 10.0.0.1
[root@jindada ~]# ll
total 35408
-rw-r--r-- 1 root root 36257792 Aug  4 11:13 etc.tar.gz
[root@jindada ~]# ll -h
total 35M
-rw-r--r-- 1 root root 35M Aug  4 11:13 etc.tar.gz
[root@jindada ~]# tar  czf  etc.tar.gz     /usr/  /etc/  /var  
tar: Removing leading `/' from member names
tar: Removing leading `/' from hard link targets
^C
[root@jindada ~]# ll -h
total 128M
-rw-r--r-- 1 root root 104M Aug  4 11:14 etc.tar.gz 

创建screen终端并执行任务

[root@jindada ~]# screen vi ~/main.c

重新连接id为2276的screen终端

[root@jindada ~]# screen -r 2276

显示已创建的screen终端

[root@jindada ~]# screen ls

将wget_mysql作业离线

[root@jindada ~]# screen -d wget_mysql

6.具体小案例

#列出screen后台运行的程序

[root@jindada ~]# screen   -list
No Sockets found in /var/run/screen/S-root.

[root@jindada ~]# 

[root@jindada ~]# screen  -S     vim		#打开一个新的窗口 并指定窗口名称 
[detached from 13659.vim]

[root@jindada ~]# screen  -list
There is a screen on:
	13659.vim	(Detached)
1 Socket in /var/run/screen/S-root.


[root@jindada ~]# screen   -r  vim		#或者通过id号 


[root@jindada ~]# screen   -xr  vim		#实时监控窗口的操作



[detached from 13659.vim]


ctrl +  a  + d   #临时退出 

exit	#真正退出 

十一、后端运行程序-nohup

1.含义

nohup命令来自于英文词组“no hang up”的缩写,其功能是用于后端运行程序。nohup命令可以将程序以忽略挂起信号的方式运行起来,而被运行程序的输出信息不会显示到屏幕上。

语法格式:nohup [参数]

2.常用参数

--help 在线帮助
--version 显示版本信息

3.常见示例

以后端模式运行指定脚本程序

[root@jindada ~]# nohup linux.sh &

以后端模式执行指定命令,并将输出结果写入到文件

[root@jindada ~]# nohup uptime &> system.txt

4.具体小案例

[root@jindada ~]# nohup   ping  www.baidu.com  &
[1] 13517
[root@jindada ~]# nohup: ignoring input and appending output to ‘nohup.out’

[root@jindada ~]# 


[root@jindada ~]# nohup   ping  www.baidu.com  &
[1] 13592
[root@jindada ~]# nohup: ignoring input and appending output to ‘nohup.out’

[root@jindada ~]# 
[root@jindada ~]# 
[root@jindada ~]# ps  aux |grep  13592
root      13592  0.0  0.0 149968  1988 pts/1    S    11:17   0:00 ping www.baidu.com
root      13594  0.0  0.0 112708   976 pts/1    R+   11:17   0:00 grep --color=auto 13592


[root@jindada ~]# jobs		#显示当前窗口的后台进程 
[1]+  Running                 nohup ping www.baidu.com &


[root@jindada ~]# vim  test.log

[2]+  Stopped                 vim test.log
[root@jindada ~]# jobs
[1]-  Running                 nohup ping www.baidu.com &
[2]+  Stopped                 vim test.log

#将后台程序放入到前台运行 

[root@jindada ~]# fg  %2


#将暂停在后台的程序继续在后台运行    输出还是输出到屏幕上面 

[root@jindada ~]# bg %2
[2]+ ping www.baidu.com &


[root@jindada ~]# ping  www.baidu.com
PING www.a.shifen.com (112.80.248.75) 56(84) bytes of data.
64 bytes from 112.80.248.75 (112.80.248.75): icmp_seq=1 ttl=128 time=16.5 ms
64 bytes from 112.80.248.75 (112.80.248.75): icmp_seq=2 ttl=128 time=13.7 ms
64 bytes from 112.80.248.75 (112.80.248.75): icmp_seq=3 ttl=128 time=13.8 ms
^Z
[1]+  Stopped                 ping www.baidu.com
[root@jindada ~]# jobs
[1]+  Stopped                 ping www.baidu.com
[root@jindada ~]# fg %1
ping www.baidu.com
64 bytes from 112.80.248.75 (112.80.248.75): icmp_seq=4 ttl=128 time=14.2 ms
64 bytes from 112.80.248.75 (112.80.248.75): icmp_seq=5 ttl=128 time=14.5 ms
64 bytes from 112.80.248.75 (112.80.248.75): icmp_seq=6 ttl=128 time=14.7 ms
^C
--- www.a.shifen.com ping statistics ---
7 packets transmitted, 6 received, 14% packet loss, time 48609ms
rtt min/avg/max/mdev = 13.700/14.604/16.559/0.955 ms

十二、系统平均负载

1.系统平均负载-top

[root@jindada ~]# top
top - 11:54:48 up 1 day,  1:52,  3 users,  load average: 0.00, 0.01, 0.05

2.系统平均负载-w

[root@jindada ~]# w
 11:54:59 up 1 day,  1:52,  3 users,  load average: 0.00, 0.01, 0.05

3.系统平均负载-uptime

3.1 含义

Linux系统中的uptime命令主要用于获取主机运行时间和查询Linux系统负载等信息。

uptime命令可以显示系统已经运行了多长时间,信息显示依次为:现在时间、系统已经运行了多长时间、目前有多少登录用户、系统在过去的1分钟、5分钟和15分钟内的平均负载。 uptime命令用法十分简单,直接输入uptime即可查看系统负载情况。

语法格式:uptime [参数]

3.2 常用参数

-p 以漂亮的格式显示机器正常运行的时间
-s 系统自开始运行时间,格式为yyyy-mm-dd hh:mm:ss
-h 显示帮助信息

3.3 常见示例

显示当前系统运行负载情况

[root@jindada ~]# uptime
 11:55:09 up 1 day,  1:53,  3 users,  load average: 0.00, 0.01, 0.05

使用-p参数显示机器正常运行的时间

[root@jindada ~]# uptime -p
up 2 days, 5 hours, 15 minutes

使用-s参数显示机器启动时间

[root@jindada ~]# uptime -s
2019-05-09 10:09:43

4.系统平均负载问题

1分钟    5分钟    15分钟   系统的平均负载 

平均负载跟系统CPU的使用率没有太大关系


在单位时间内,系统正在运行中的进程数或者不可中断的进程数量   进程的活跃数量 


跟CPU的核心数有关系 


当负载为2的时候    

核心数为4       50% 

核心数为2		100% 

核心数为1       200%



当1分钟的值小于5分钟和15分钟的时候   说明你之前的平均负载过高,当前正在降低  

当1分钟跟5分钟和15分钟的值很近的时候,平均负载很稳定  

当1分钟的值大于5分钟和15分钟的时候,说明现在的负载正在慢慢的变大   


当值达到70%   就要开始分析问题 
posted @ 2020-08-05 15:52  年少纵马且长歌  阅读(310)  评论(0编辑  收藏  举报