linux进程管理

进程和线程的关系

以下介绍为linux环境

  • 进程是操作系统中一个运行中的程序,是资源分配和调度的基本单位。每个进程有自己独立的内存空间、文件描述符、堆栈等系统资源

  • 线程(Thread) 是 CPU 调度的最小单位,是进程中的一个执行流。同一进程中的多个线程可以共享资源(如全局变量、堆内存),并且线程之间通信和数据共享比进程容易。

ps命令查看进程

PS 参数说明
-a显示所有用户的进程(而不仅仅是当前用户的进程),包括没有控制终端的进程
-e查看所有进程,相当于-aux,所以有了ps -ef
-l列出更多信息,长格式输出
-t仅显示与指定终端相关的进程。示例:ps -t tty1 -auf
-u显示用户相关的进程信息,包括用户名、CPU 使用率、内存使用率、进程的启动时间
-x显示所有进程,包括没有控制终端的进程(即后台进程)
-f显示进程树(forest),以树形结构呈现进程之间的父子关系

ps -auxf查看效果图,进程直观地用树形表示出来

在这里插入图片描述

ps -ef查看所有进程

root     16855     1  0 Nov11 ?        00:00:00 nginx: master process nginx
www      16856 16855  0 Nov11 ?        00:00:02 nginx: worker process
www      16857 16855  0 Nov11 ?        00:00:00 nginx: worker process
root     20586     1  0 Aug06 ?        00:00:00 login -- root
www      21843 25724  0 Nov05 ?        00:00:15 php-fpm: pool www
root     21913     1  0 Nov04 ?        00:11:38 /usr/local/share/aliyun-assist/2.2.3.843/aliyun-service
root     21929     1  0 Nov04 ?        00:01:58 /usr/local/share/assist-daemon/assist_daemon
mysql    23055     1  0 Jun12 ?        00:00:00 /bin/sh /usr/bin/mysqld_safe --basedir=/usr
mysql    23221 23055  0 Jun12 ?        01:55:38 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/pl
root     25724     1  0 Oct18 ?        00:01:28 php-fpm: master process (/etc/php-fpm.conf)
www      25725 25724  0 Oct18 ?        00:04:35 php-fpm: pool www
www      25726 25724  0 Oct18 ?        00:02:33 php-fpm: pool www
www      25727 25724  0 Oct18 ?        00:00:30 php-fpm: pool www
www      25728 25724  0 Oct18 ?        00:02:29 php-fpm: pool www
www      25729 25724  0 Oct18 ?        00:00:30 php-fpm: pool www
www      27261 25724  0 Oct18 ?        00:00:30 php-fpm: pool www
root     29152     1  0 Oct15 ?        00:03:36 /usr/bin/dockerd
root     29159 29152  0 Oct15 ?        00:48:20 containerd --config /var/run/docker/containerd/containerd.toml --log-level info
www      29861 25724  0 Oct19 ?        00:02:30 php-fpm: pool www
root     31905     1  0 Jul14 ?        00:00:18 /usr/sbin/sshd -D

PPid查看父进程号

1个mysql进程,怎么找到它的父进程。可以用到PPid,首先根据进程名字找到它的pid号,再根据pid号查找它的父进程

[root@db01~]# ps -ef|grep mysql
root      10976      1  0 22:20 pts/4  
mysql     11144  10976  0 22:20 pts/4

[root@db01~]# grep PPid /proc/11144/status
PPid:	10976

pstree树形化显示所有进程

pstree参数说明
-p列出pid号

在这里插入图片描述

pstree此查看一个进程的父进程比较方便

[root@centos7 ~]# pstree  -p | grep 3509
           |-gvfsd(2963)-+-gvfsd-burn(3509)-+-{gvfsd-burn}(3510)	#父进程是2963

进程的状态

ps 命令中的 -u 参数可以列出进程的状态

[root@centos7 ~]# ps -aux | head
USER        PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root          8  0.0  0.0      0     0 ?        S    04:45   0:00 [rcu_bh]
状态说明
R(running)正在运行的进程
S已中断的进程,进程已经停止运行。可以由外部信号(如 SIGSTOP)或内部条件(如进程调用了 pause())导致。停止状态下的进程通常是暂停的,不会继续执行,直到接收到继续执行的信号(如 SIGCONT
T(terminate )进程被挂起,如Ctrl+Z把进程放到后台。ps -aux
D不可中断进程,正在读写的进程
Z僵尸进程
W (Paging)程处于等待分页操作的过程中,通常是因为内存不足,进程等待交换到磁盘。系统内存严重不足时出现

状态右边的符号含义:

字符含义示例
+前台进程S+, R+, T+
s父进程Ss, S+
l多线程进程Rl, Sl
<高优先级进程S<, R<
N低优先级进程RN, S
L锁定内存SL, RL
T停止进程T+, T
Z僵尸进程Z+, Z
W进程正在写入S+, RW

僵尸进程

在这里插入图片描述

僵尸进程介绍:

  • 僵尸进程产生:子进程结束,但是其父进程还没有回收子进程的资源

  • 僵尸进程危害:占用着系统资源,过多的僵尸进程会导致CPU、内存等占用过高。

排查僵尸进程的方式:

方式:top查找zombie进程

[root@centos7 ~]# top		#在第二行0zombie,表示没有僵尸进程
top - 13:46:06 up  1:27,  1 user,  load average: 0.02, 0.02, 0.05
Tasks: 180 total,   1 running, 179 sleeping,   0 stopped,   0 zombie

方式2:ps -aux | grep Z也可以查看

[root@centos7 ~]# ps aux | grep Z	#如果STAT为Z+就是僵尸进程
USER        PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root       3739  0.0  0.0 112828   952 pts/0    Z+   14:42   0:00 grep --color=auto Z

解决zombie进程的方法

僵尸进程无法通过kill(kill -9)命令结束

  • 方法一:结束僵尸进程的主进程/父进程

  • 方法二:如果主进程为1,则重启linux系统

孤儿进程

在这里插入图片描述

  • 孤儿进程的产生:子进程的父进程挂了

  • 孤儿进程会被系统接管,仍能继续运行

kill 命令结束进程

结束进程说明
kill+pid,结束进程,常用
pkill模糊查找进程名并结束,慎用
killall精确查找进程名
kill -9强制杀死进程

让程序后台运行

  • Ctrl + Z,把后台挂起,此时进程就会处于 T 状态
  • jobs可以查看道后台挂起的进程有哪些
  • fg命令恢复为前台运行
[root@qwe~]# ping jd.com 
PING jd.com (111.13.149.108) 56(84) bytes of data.
64 bytes from 111.13.149.108 (111.13.149.108): icmp_seq=1 ttl=47 time=48.6 ms
64 bytes from 111.13.149.108 (111.13.149.108): icmp_seq=2 ttl=47 time=48.6 ms
^Z # 按一下 Ctrl + Z 
[1]+  Stopped                 ping jd.com

# 可以看到 进程状态为 T ,也就是中断了
[root@qwe~]# ps -u | grep ping
root     12204  0.0  0.1 150092  2004 pts/0    T    19:22   0:00 ping jd.com
root     12224  0.0  0.0 112812   980 pts/0    S+   19:22   0:00 grep --color=auto ping

# 列出了之前的ping
[root@qwe~]# jobs
[1]+  Stopped                 ping jd.com

# 使用fg后,又回到前台运行了
[root@qwe~]# fg
ping jd.com
64 bytes from 111.13.149.108 (111.13.149.108): icmp_seq=3 ttl=47 time=48.3 ms
64 bytes from 111.13.149.108 (111.13.149.108): icmp_seq=4 ttl=47 time=47.9 ms
64 bytes from 111.13.149.108 (111.13.149.108): icmp_seq=5 ttl=47 time=47.9 ms

nohup命令

nohub用于运行指定的命令或脚本,并忽略挂起(SIGHUP)信号。即使关闭了终端或断开了 SSH 连接,该命令或脚本也会继续在后台运行

常用方法如:nohub java -jar app.jar &,这样即使退出了终端,程序也会在后台运行。

使用 nohub 和 & 后,ping命令持续运行,一直往文件中输出

[root@qwetmp]# nohup ping jd.com &
[1] 12543

[root@qwetmp]# wc -l nohup.out 
46 nohup.out
[root@qwetmp]# wc -l nohup.out 
49 nohup.out
[root@qwetmp]# wc -l nohup.out 
50 nohup.out

# 
[root@qwetmp]# ps -u | grep ping
root     12543  0.0  0.1 150092  2012 pts/0    S    19:27   0:00 ping jd.com
root     12741  0.0  0.0 112812   980 pts/0    S+   19:30   0:00 grep --color=auto ping

在命令后加上&可放到后台运行, nohup ping baidu.com -c 3 &把运行结果写入到nohub.out文件中。

screen虚拟窗口

screen命令说明
-S name1进入子shell
-ls查看 screen 号,
-r配合screen号回到screen
Ctrl + A + D平滑退出screen
  • screen会创建一个虚拟空间,只要这个空间存在,里面运行的命令就不会消失

  • 用法:输入screen命令进入一个虚拟终端,然后执行命令

  • screen可以嵌套

# 创建第1个虚拟空间,然后当前终端就会自动进入
[root@qwe~]# screen -S name1

# 在第1个虚拟空间内创建第2个虚拟空间
[root@qwe~]# screen -S name2

# 查看当前进程,可以看到2个 screen。当前处于第2个screen内。
[root@qwe~]# ps -auf
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root     14064  1.2  0.1 116632  3036 pts/2    Ss   19:47   0:00 /bin/bash
root     14084  0.0  0.1 155448  1860 pts/2    R+   19:47   0:00  \_ ps -auf
root     14034  0.4  0.1 116632  3204 pts/1    Ss   19:47   0:00 /bin/bash
root     14061  0.7  0.0 127744  1188 pts/1    S+   19:47   0:00  \_ screen -S name2
root     13986  0.0  0.1 116604  3164 pts/0    Ss   19:47   0:00 -bash
root     14032  0.1  0.0 127744  1188 pts/0    S+   19:47   0:00  \_ screen -S name1


  3204 pts/1    Ss   19:47   0:00 /bin/bash
root     14061  0.7  0.0 127744  1188 pts/1    S+   19:47   0:00  \_ screen -S name2
root     13986  0.0  0.1 116604  3164 pts/0    Ss   19:47   0:00 -bash
root     14032  0.1  0.0 127744  1188 pts/0    S+   19:47   0:00  \_ screen -S name1


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值