六、进程管理、端口和作业控制

2020-01-24  本文已影响0人  胖虎喜欢小红

一、关于进程 process

1.1. 什么是进程?

进程是已启动的可执行程序的运行实例

程序: 二进制文件,静态 /bin/date, /usr/sbin/httpd,/usr/sbin/sshd, /usr/local/nginx/sbin/nginx
进程: 是程序运行的过程, 动态,有生命周期及运行状态。

进程状态

在多任务处理操作系统中,每个CPU在一个时间点上只能处理一个进程。在进程运行时,它对
CPU 时间和资源分配的要求会不断变化,从而为进程分配一个状态,它随着环境要求而改变。
1.2 .查看进程 process

静态查看进程

[root@biudefor ~]# ps aux | less
参数解释:
ps :process nsapashot
a 只能查看系统里面运行的所有终端进程
u 显示进程拥有者
x 显示系统内所有进程
f 显示进程之间的父子关系
-------------------
less: 可以上下翻页
[root@biudefor ~]# ps aux 
USER        PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
============================================================================
USER:    #运行进程的用户
PID:    #进程ID
%CPU:    #CPU占用率
%MEM:    #内存占用率
VSZ:     #虚拟内存集,进程占用的虚拟内存空间
RSS:     #物理内存集,进程占用实际物理内存空间
STAT:   #进程状态    man ps (/STATE)
?   表示没有占用终端
R   运行
S   可中断睡眠 Sleep
D   不可中断睡眠 (usually IO)
T   停止的进程 
Z   僵尸进程
X   死掉的进程
START:      #进程的启动时间
TIME:      #进程占用CPU的总时间
COMMAND:   #进程文件,进程名
查看tty的方法:
[root@biudefor ~]# tty
? 表示这个进程开启的时候没有占用终端

查看进程(二)

[root@biudefor ~]# ps -ef
参数解释:
-e 显示所有进程
-l 长格式显示
-f 完整格式

按指定字段排序

[root@biudefor ~]# ps aux --sort %cpu | less  #从小到大
[root@biudefor ~]# ps aux --sort -%cpu | less #从大到小
--sort:排序

查看单个PID

[root@biudefor ~]# yum install -y httpd  #安装apache软件
[root@biudefor ~]# systemctl start httpd #启动
[root@biudefor ~]# cat /var/run/httpd/httpd.pid 
70646

查看指定PID

[root@biudefor ~]# ps aux | grep sshd
root       1043  0.0  0.2 105996  4120 ?        Ss   01:32   0:00 /usr/sbin/sshd -D
grep:过滤
1.3 动态查看进程

top、htop

[root@biudefor ~]# top
image.png
在工作中必须监控的东西 load average(平均负载)等待cpu处理的队列长度 也是个数。
1分钟   第一个数字
5分钟   第二个数字
15分钟  第三个数字
======================
计算CPU负载: load average: 0.01, 0.05, 0.05  /  CPU的个数,超过1就是负载过大

top操作

[root@biudefor ~]# top
# 1 查看CPU个数
h|? 帮助
> 往下翻页
< 往上翻页
M 按内存排序
P 按cpu排序
q 退出   
z 彩色显示
r 调整进程的nice优先级
W 保存
f 调整显示栏
=============================
进程状态了解
Sl  以线程的方式运行
Ss  s进程的领导者,父进程
R+  +表示是前台的进程组
S< <优先级较高的进程    
SN  N优先级较低的进程
image.png
ni: 优先级
id: cpu空闲率
wa: cpu等待,如果使用率过高,表示硬盘该换了
进程控制

按pid杀死进程

kill,pkill
语法: kill 信号 PID   #信号也是进程间通信的一种方式
#  pkill -9 httpd   // 杀死所有httpd进程
#  kill -9 pid   //仅杀死pid所有者进程
#  能stop的就不使用kill 
[root@biudefor ~]# kill -l   #查看所有信号
-1   HUP  重新加载进程或者重新加载配置文件,PID不变
-9   KILL 强制杀死
-15  TERM 正常杀死(这个信号可以默认不写)# 无法杀死僵停的进程
-18  CONT 激活进程
-19  STOP 挂起进程

案例一

给vsftpd进程发送信号1,15 vsftpd信号测试

[root@biudefor ~]# yum install -y vsftpd  #安装vsftpd
[root@biudefor ~]# systemctl start vsftpd  #启动
[root@biudefor ~]# ps aux | grep vsftpd
root      59363  0.0  0.0  53212   576 ?        Ss   16:47   0:00 /usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf

[root@biudefor ~]# kill -1 59363  #发送重启信号,例如vsftpd的配置文件发生改变,希望重新加载
[root@biudefor ~]# ps aux | grep vsftpd
root      59363  0.0  0.0  53212   748 ?        Ss   16:47   0:00 /usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf
[root@biudefor ~]# kill 59363 #正常杀死进程,信号为-15可以默认不写。我们可以使用systemctl stop vsftpd 停止服务。
[root@biudefor ~]# ps aux | grep vsftpd
root      62493  0.0  0.0 112660   968 pts/0    S+   16:51   0:00 grep --color=auto vsftpd

参数解释:
+: 表示运行在前台的进程组
S+:休眠状态
T+:暂停,挂起状态
s: 父进程

案例二

给vsftpd进程发送信号-9, vsftpd信号测试

[root@biudefor ~]# systemctl start vsftpd
[root@biudefor ~]# ps aux | grep vsftpd
root      67003  0.0  0.0  53212   572 ?        Ss   16:57   0:00 /usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf
root      67089  0.0  0.0 112660   972 pts/0    S+   16:57   0:00 grep --color=auto vsftpd
[root@biudefor ~]# kill -9 67003  #强制杀死,一般用于不能正常停止的情况下
[root@biudefor ~]# ps aux | grep vsftpd
root      67190  0.0  0.0 112660   972 pts/0    S+   16:57   0:00 grep --color=auto vsftpd

案例三

使用pkill 杀死vsftpd进程

[root@biudefor ~]# systemctl start vsftpd
[root@biudefor ~]# ps -aux | grep vsftpd
root      73399  0.0  0.0  53212   572 ?        Ss   17:05   0:00 /usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf
root      73499  0.0  0.0 112660   968 pts/0    S+   17:05   0:00 grep --color=auto vsftpd
[root@biudefor ~]# pkill -9 vsftpd  #使用pkill可以指定进程名字
[root@biudefor ~]# ps -aux | grep vsftpd
root      73643  0.0  0.0 112660   968 pts/0    S+   17:05   0:00 grep --color=auto vsftpd

示例

信号测试18,19
[root@biudefor ~]# ps aux | grep sshd 
root       1043  0.0  0.2 105996  4120 ?        Ss   01:32   0:00 /usr/sbin/sshd -D
[root@biudefor ~]# kill -19 1043   #将进程挂起
[root@biudefor ~]# ps aux | grep sshd 
root       1043  0.0  0.2 105996  4120 ?        Ts   01:32   0:00 /usr/sbin/sshd -D
[root@biudefor ~]# kill -18 1043   #将进程激活
[root@biudefor ~]# ps aux | grep sshd 
root       1043  0.0  0.2 105996  4120 ?        Ss   01:32   0:00 /usr/sbin/sshd -D
进程优先级 nice
nice 值越高:表示优先级越低,例如+19,该进程容易将CPU 使用量让给其他进程。
nice 值越低:表示优先级越高,例如-20,该进程更不倾向于让出CPU。
# NI + 20 =PR

查看进程优先级

[root@biudefor ~]# ps axo pid,command,nice  --sort=-nice | less
o:指定字段
image.png
[root@biudefor ~]# nice --15 vim &   # & 在后台挂起 
                             -15

修改进程优先级

[root@biudefor ~]# ps -ef | grep vim 
root      92485  28819  0 17:30 pts/0    00:00:00 vim
root      94300  28819  0 17:32 pts/0    00:00:00 grep --color=auto vim
[root@biudefor ~]# renice 10 92485
92485 (process ID) old priority -15, new priority 10

二、查看端口

[root@biudefor ~]# yum install lsof  #安装软件包
[root@biudefor ~]# lsof -i:80        #端口号,这能查看带端口的进程
COMMAND   PID   USER   FD   TYPE  DEVICE SIZE/OFF NODE NAME
httpd   64249   root    4u  IPv6 1373628      0t0  TCP *:http (LISTEN)监听
#查网络进程和正在监听的端口
[root@biudefor ~]# netstat -lntp
参数详解:
-a   显示全部的进程
-u   显示udp
-n   以数字的新式显示协议名称
-t   tcp
-p   显示进程的名称和pid
-l   只显示正在被监听的端口
[root@biudefor ~]# ss -lnta
-a   显示所有的套接字
-l   显示监听套接字
-n   不解析服务名称
-t   只显示TCP套接字
-p   使用socket显示进程
-u   只显示UDP套接字
[root@biudefor ~]# w  #看已经登陆到终端的进程信息,远程登陆会有ip 地址
[root@biudefor ~]# who

三、作业控制

作业控制之jobs:

实战案例

[root@linux-server ~]# vim a.txt &   #放后台运行
[1] 101839
[root@linux-server ~]# jobs  #查看工作号
[1]-  Stopped                 vim a.txt
[root@linux-server ~]# fg %1  #把程序调到前台,%是用来修饰job number,1就是job number。(程序的工作号)
# ctrl+z  #把程序放到后台(这方法会让程序在后台暂停)
[root@linux-server ~]# bg %1  #让暂停的程序在后台运行
[1]+  Stopped                 vim a.txt
[root@linux-server ~]# kill -9 %1 #杀死程序
[1]+  Stopped                 vim a.txt
[root@linux-server ~]# jobs  #再次查看为空
上一篇下一篇

猜你喜欢

热点阅读