Linux下的进程管理

#####5.1什么是进程#############
进程(Process)
1.程序是静态的文件
2.进程是运行中的程序的一个副本
3.进程存在生命周期(准备期,运行期,终止期)

 

#####5.2进程状态################

         状态                       定义
   R(TASK_RUNNING)         可执行状态(RUNNING,READY)
S(TASK_INTERRUPTIBLE)          可唤醒睡眠状态  
D(TASK_UNINTERRUPTIBLE)       不可唤醒睡眠状态
   T(TASK_STOPPED)                暂停状态
    z(EXIT_ZOMBIE)                 僵死态


[[email protected] Desktop]# gedit       ##编辑gedit
^Z                                      ##打入后台(虚拟机会死机)
[1]+  已停止               gedit

Linux下的进程管理

Linux下的进程管理

#####5.3进程查看##############
1.pstree
[[email protected] Desktop]# pstree      ##查看进程树

Linux下的进程管理


2.ps命令的常规用法
显示当前命令执行时进程状态
a       显示与终端相关的进程(BSD)
[[email protected] Desktop]# ps a

Linux下的进程管理

x       显示所有与终端无关的进程(BSD)
[[email protected] Desktop]# ps x

Linux下的进程管理

u       以用户为归类来显示进程状态信息(BSD)
[[email protected] Desktop]# ps uax | less
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.1  0.7 244588 13368 ?        Ss   05:18   0:02 /usr/lib/systemd/systemd --switched-root --system --deserialize 17
root         2  0.0  0.0      0     0 ?        S    05:18   0:00 [kthreadd]
root         3  0.0  0.0      0     0 ?        I<   05:18   0:00 [rcu_gp]

Linux下的进程管理

Linux下的进程管理

f       层级结构显示进程信息
[[email protected] Desktop]# bash        ##新开一个bash
[[email protected] Desktop]# ps f
  PID TTY      STAT   TIME COMMAND
 3564 pts/1    Ss     0:00 bash
 6791 pts/1    S      0:00  \_ bash
 6829 pts/1    R+     0:00      \_ ps f

Linux下的进程管理

o       指定查看选项,pid,comm,nice,pri,pcpu,stat,ppid
[[email protected] Desktop]# ps axo pid
[[email protected] Desktop]# ps axo pid,comm

Linux下的进程管理

Linux下的进程管理

-e      显示所有进程(uinx)
[[email protected] Desktop]# ps -e | less

Linux下的进程管理

Linux下的进程管理

-f      显示完整格式信息(unix)
[[email protected] Desktop]# ps -e -f| less
UID        PID  PPID  C STIME TTY          TIME CMD ##所属用户,进程名称,副级进程,CPU用量,开启时间,运行终端,CPU处理进程时间,进程名词
root         1     0  0 05:18 ?        00:00:02 /usr/lib/systemd/systemd --switched-root --system --deserialize 17
root         2     0  0 05:18 ?        00:00:00 [kthreadd]
root         3     2  0 05:18 ?        00:00:00 [rcu_gp]
root         4     2  0 05:18 ?        00:00:00 [rcu_par_gp]

Linux下的进程管理

Linux下的进程管理

-H      层级结构显示进程的相关信息
[[email protected] Desktop]# ps -e -f -H| less

Linux下的进程管理

Linux下的进程管理

-o      指定查看选项,pid,comm,nice,%cpu,%mem,nice
[[email protected] Desktop]# ps -eo pid,user,group,comm,%cpu

Linux下的进程管理

注意:

[[email protected] Desktop]# ps ax  ##所有进程

Linux下的进程管理
[[email protected] Desktop]# ps ax | less        ##分页浏览
 PID TTY      STAT   TIME COMMAND       ## 进程ID   进程所使用到的终端  进程的状态   进程所占用CPU的总时间   进程的名字
    1 ?        Ss     0:02 /usr/lib/systemd/systemd --switched-root --system --deserialize 17
    2 ?        S      0:00 [kthreadd]
    3 ?        I<     0:00 [rcu_gp]
    4 ?        I<     0:00 [rcu_par_gp]
    6 ?        I<     0:00 [kworker/0:0H-kblockd]

Linux下的进程管理

Linux下的进程管理

ps命令显示信息
  字段           解释
 USER          进程所有人
 PID           进程的ID
 %CPU          进程所占用CPU%
 %MEM          所占用内存%
 VSZ           虚拟内存占用量
 RSS           常驻内存容量
 TTY           所使用终端
 STAT          进程状态
 START         程序开启时间
 TIME          占用CPU总时间
 COMMAND       进程名字

[[email protected] Desktop]# ps axo pid,comm,%cpu --sort=%cpu ##sort 排序  正排序

Linux下的进程管理
[[email protected] Desktop]# ps axo pid,comm,%cpu --sort=-%cpu   ##倒排序

Linux下的进程管理


3.进程过滤命令pgrep
-u  uid   显示指定用户进程
[[email protected] Desktop]# su - student        ##新开一个shell进入student用户
[[email protected] ~]$ id student             ##查看用户id
uid=1000(student) gid=1000(student) 组=1000(student),10(wheel)
[[email protected] ~]$ ps
  PID TTY          TIME CMD
 7682 pts/2    00:00:00 bash
 7727 pts/2    00:00:00 ps
[[email protected] Desktop]# pgrep -u 1000       ##过滤1000
7682

Linux下的进程管理

-U  name  显示指定用户进程
[[email protected] Desktop]# pgrep -l -U student
7682 bash

Linux下的进程管理

-t  tty   显示指定终端进程
[[email protected] Desktop]# pgrep -l -t pts/2
7625 bash
7681 su
7682 bash

Linux下的进程管理

-l        显示进程名称
[[email protected] Desktop]# pgrep -l -u 1000
7682 bash

Linux下的进程管理

-a        显示完整格式进程名称
[[email protected] Desktop]# pgrep -al -t pts/2
7625 bash
7681 su - student
7682 -bash

Linux下的进程管理

-P  pid   显示指定进程的子进程
[[email protected] Desktop]# pgrep -P 1

Linux下的进程管理


4.pidof
根据进程名称获取pid
pid  vim

[[email protected] Desktop]# watch -n 1 date     ##运行一个watch

Linux下的进程管理
[[email protected] Desktop]# pidof watch         ##查看watch进程id
3244

Linux下的进程管理
[[email protected] Desktop]# ps aux | grep watch ##验证
root      3244  0.0  0.1 223756  2764 pts/2    S+   05:11   0:00 watch -n 1 date

Linux下的进程管理


5.top
top内部参数
P    cpu百分比排序

Linux下的进程管理
M    内存百分比

Linux下的进程管理
T    累计占用cpu时间

Linux下的进程管理
l    开启/关掉uptime信息

Linux下的进程管理

Linux下的进程管理
t    开启/关闭task&cpu信息

Linux下的进程管理

Linux下的进程管理

Linux下的进程管理

Linux下的进程管理


s    调整刷新频率

Linux下的进程管理
k    终止进程

Linux下的进程管理

Linux下的进程管理
u    查看指定用户进程
[[email protected] Desktop]# su - student        ##新开一个shell切换至student
上一次登录:三 1月 29 08:47:30 EST 2020pts/2 上

Linux下的进程管理

Linux下的进程管理

top命令参数
-d   指定刷新间隔
[[email protected] Desktop]# top -d 1    ##1s刷新一次

Linux下的进程管理

-b   以批次方式显示
[[email protected] Desktop]# top -b -d1  ##1s显示一次批次显示

Linux下的进程管理
-n   显示批次数量
[[email protected] Desktop]# top -b -d1 -n 2 ##刷新2次

Linux下的进程管理

注意:
[[email protected] Desktop]# top         ##默认三秒刷新一次

Linux下的进程管理

Linux下的进程管理
top - 05:23:07 ##当前系统时间
up 42 min       ##当前系统运行时间
1 user          ##当前有多少用户
load average: 0.02, 0.04, 0.00  ##系统负载 1min  5min 15min
Tasks: 251 total ##任务总量
1 running 249 sleeping 0 stopped 0 zombie ##正在运行个数 睡眠状态  暂停状态  僵死态
%Cpu(s):2.8 us,1.2 sy,0.0 ni,95.7 id,0.0 wa,0.2 hi,0.0 si,0.2 st ##cpu使用量 用户 内核 进程优先级调整 空闲 等待输入输出时间 硬中断 软件中断 被偷走的cpu量

MiB Mem :1829.1 total,65.2 free, 1064.3 used,699.5 buff/cache ##物理内存量 总量 空闲量 使用量 缓存量(buff/cache(等待写入磁盘的数据/从磁盘读取的数据))
MiB Mem :   1829.1 total,     65.2 free,   1064.3 used,    699.5 buff/cache ##交换分区占用

PID  进程ID
USER 用户名称
PR   优先级
NI   优先级nice
VIRT 虚拟cpu
RES  常驻内存地址
SHR  共享资源
S    状态
%CPU cpu用量
%MEM 内存用量
TIME+占用cpu的时间
COMMAND 名称

 

#####5.4进程优先级###########
1.按照资源占用划分进程类型
  资源使用量         进程类型  
 cpu使用密集        CPU-Bound
 I/O使用密集        I/O-Bound
2.优先级范围
对Linux而言优先级被划分成固定个数(0-139)
1-99为实时优先级,数字越大优先级越高,不用手动处理系统自用
100-139为静态优先级,数字越小优先级越高

3.NICE值
NICE值:-20-19对应静态优先级
普通用户只可以调低优先级,超级用户任意调整
关于优先级的命令
-ps   ax   -o   nice,pid,comm
[[email protected] Desktop]# ps -o nice,pid,comm
 NI   PID COMMAND
  0  2618 bash
  0  5100 ps

Linux下的进程管理

-nice   -n   优先级   程序
[[email protected] Desktop]# vim &
[2] 5295
[[email protected] Desktop]# ps -o stat,nice,pid,comm
STAT  NI   PID COMMAND
Ss     0  2618 bash
TN    10  5141 vim
T      0  5295 vim
R+     0  5302 ps

[2]+  已停止               vim
[[email protected] Desktop]# nice -n  5 vim &    ##开启进程指定优先级
[3] 5326
[[email protected] Desktop]# ps -o stat,nice,pid,comm
STAT  NI   PID COMMAND
Ss     0  2618 bash
TN    10  5141 vim
T      0  5295 vim
TN     5  5326 vim
R+     0  5333 ps

[3]+  已停止               nice -n 5 vim

Linux下的进程管理

-renice   -n   优先级   程序.pid
[[email protected] Desktop]# vim &
[1] 5141
[[email protected] Desktop]# ps -o nice,pid,comm
 NI   PID COMMAND
  0  2618 bash
  0  5141 vim
  0  5148 ps

[1]+  已停止               vim
[[email protected] Desktop]# renice -n -10 5141  ##调整vim优先级为-10
5141 (process ID) 旧优先级为 0,新优先级为 -10
[[email protected] Desktop]# ps -o nice,pid,comm
 NI   PID COMMAND
  0  2618 bash
-10  5141 vim
  0  5170 ps

Linux下的进程管理


注意:
[[email protected] Desktop]# ps -o stat,nice,pid,comm
STAT  NI   PID COMMAND
Ss     0  2618 bash
T<   -10  5141 vim      ##<进程优先级高于默认
R+     0  5204 ps

Linux下的进程管理

[[email protected] Desktop]# renice -n 10 5141
5141 (process ID) 旧优先级为 -10,新优先级为 10
[[email protected] Desktop]# ps -o stat,nice,pid,comm
STAT  NI   PID COMMAND
Ss     0  2618 bash
TN    10  5141 vim      ##N进程优先级低于默认
R+     0  5244 ps

Linux下的进程管理


#####5.5进程前后台调用#########
  指令              含义
 jobs           查看后台任务
ctrl+z      将前台运行进程打入后台
  bg            **后台进程
  fg             调回进程
  &              运行在后台

[[email protected] Desktop]# gedit       ##ctrl+z打入后台
^Z
[1]+  已停止               gedit
[[email protected] Desktop]# ps -o stat,comm
STAT COMMAND
Ss   bash
Tl   gedit
R+   ps

Linux下的进程管理

Linux下的进程管理
[[email protected] Desktop]# vim         ##在开一个进程并将其打入后台

[2]+  已停止               vim
[[email protected] Desktop]# jobs        ##查看后台任务
[1]-  已停止               gedit
[2]+  已停止               vim

Linux下的进程管理
[[email protected] Desktop]# bg 1        ##**后台任务
[1]- gedit &
[[email protected] Desktop]# jobs
[1]-  运行中               gedit &
[2]+  已停止               vim

Linux下的进程管理

Linux下的进程管理

[[email protected] Desktop]# fg 2        ##调回后台进程

Linux下的进程管理

Linux下的进程管理
[[email protected] Desktop]# gedit &     ##使进程在后台运行
[1] 5763

Linux下的进程管理

 


#####5.6信号##############
man 7 signal
可控信号类型             含义
    1                    刷新
    2                中断键盘输入(ctrl+c)
    3                  退出键盘  (ctrl+\)
    9                  强制结束
   15                  正常关闭
   18                  **进程
   19                  强制暂停
   20                  正常暂停
[[email protected] Desktop]# kill -2 2618        ##2

Linux下的进程管理
[[email protected] Desktop]# kill -15 6289       ##15

Linux下的进程管理

[[email protected] Desktop]# kill -19 2618       ##19

Linux下的进程管理

[[email protected] Desktop]# kill -18 2618       ##18

Linux下的进程管理


信号指令:
kill  信号 pid

[[email protected] Desktop]# ps  ##打开多个vim
  PID TTY          TIME CMD
 2618 pts/1    00:00:00 bash
 6624 pts/1    00:00:00 vim
 6631 pts/1    00:00:00 vim
 6638 pts/1    00:00:00 vim
 6645 pts/1    00:00:00 ps
[[email protected] Desktop]# killall -9 vim ##结束所有vim

Linux下的进程管理
[[email protected] Desktop]# ps
  PID TTY          TIME CMD
 2618 pts/1    00:00:00 bash
 6915 pts/1    00:00:00 ps
[2]   Killed                  vim
[3]-  Killed                  vim
[4]+  Killed                  vim


pkill 信号 进程名称
[[email protected] Desktop]# su - student ##切换用户
上一次登录:四 1月 30 05:56:03 EST 2020pts/2 上
[[email protected] Desktop]# pkill -u 1000 ##结束用户
[[email protected] ~]$ 注销

Linux下的进程管理

 

#####5.7守护进程systemd########

一.设定实验环境
1.设定网络
[[email protected] Desktop]# nm-connection-editor  

Linux下的进程管理

Linux下的进程管理
-删除原有的以太网

Linux下的进程管理
-添加一个新的以太网

Linux下的进程管理
-输入连接名称

Linux下的进程管理
-添加设备

Linux下的进程管理
-IPV4方法手动

Linux下的进程管理
-添加地址(网关没有但是需要点一下)

Linux下的进程管理
[[email protected] Desktop]# ip addr show ##查看ip

Linux下的进程管理
[[email protected] Desktop]# ping 172.25.254.8   ##ping主机

Linux下的进程管理

[[email protected] Desktop]$ ssh -l root 172.25.254.178 ##在真机中连入网址

Linux下的进程管理
[[email protected] ~]# useradd westos ##新建用户westos
[[email protected] ~]# su - westos

Linux下的进程管理
[[email protected] ~]$ ssh -l root 172.25.254.178      ##建立连接
The authenticity of host '172.25.254.178 (172.25.254.178)' can't be established.
ECDSA key fingerprint is SHA256:pOQ1OJmyF2PFI+jxyFoOSCfi+1oWNsUruy2DZNjg+N0.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.25.254.178' (ECDSA) to the list of known hosts.
[email protected]'s password:
Activate the web console with: systemctl enable --now cockpit.socket

Last login: Sat Feb  1 05:59:59 2020

Linux下的进程管理
[[email protected] ~]# cd Desktop/    ####进入桌面建立文件
[[email protected] Desktop]# touch file{1..5}

Linux下的进程管理


守护进程的定义:
1.守护进程就是通常讲的Daemon进程
2.是linux后台执行的服务进程
3.是独立于控制终端,周期性地执行某种任务或等待处理某些事件
4.不会随终端关闭而停止,直到接受停止信息才会结束

 

守护进程的类型:
守护进程的类型         使用系统及特点
  Sysv int           经典的守护进程类型沿用到rhel6
  upstart            做到并行启动rhel6中应用
  systemd            独立完成启动,rhel7中使用

systemctl用法:
指令
-systemctl list-unit-files     ##查看服务开机运行情况
[[email protected] Desktop]# systemctl list-unit-files --type=service

Linux下的进程管理

Linux下的进程管理

-systemctl list-units          ##查看服务当前运行情况
[[email protected] Desktop]# systemctl list-units --type=service

Linux下的进程管理

Linux下的进程管理

-systemctl list dependencies   ##查看服务依赖性
[[email protected] Desktop]# systemctl list-dependencies sshd

Linux下的进程管理

-systemctl status service      ##查看服务状态
[[email protected] Desktop]# systemctl status sshd

Linux下的进程管理

-systemctl start service       ##开启服务
[[email protected] Desktop]# systemctl start sshd
[[email protected] Desktop]# systemctl status sshd

Linux下的进程管理

-systemctl stop service        ##关闭服务
[[email protected] Desktop]# systemctl stop sshd
[[email protected] Desktop]# systemctl status sshd

Linux下的进程管理

-systemctl reload service      ##重新加载服务配置
[[email protected] Desktop]# systemctl reload sshd
[[email protected] Desktop]# systemctl status sshd

Linux下的进程管理

-systemctl restart service     ##重新启动服务
[[email protected] Desktop]# systemctl restart sshd
[[email protected] Desktop]# systemctl status sshd

-systemctl enable service      ##设定服务开启启动
[[email protected] Desktop]# systemctl enable sshd
Created symlink /etc/systemd/system/multi-user.target.wants/sshd.service → /usr/lib/systemd/system/sshd.service.
[[email protected] Desktop]# systemctl status sshd

Linux下的进程管理

-systemctl enable--now service  ##设定服务开机启动并开启服务
[[email protected] Desktop]# systemctl stop sshd      ##关闭
[[email protected] Desktop]# systemctl disable sshd   ##开机关闭
[[email protected] Desktop]# systemctl enable --now   sshd
Created symlink /etc/systemd/system/multi-user.target.wants/sshd.service → /usr/lib/systemd/system/sshd.service.
[[email protected] Desktop]# systemctl status sshd

Linux下的进程管理

-systemctl disable service     ##设定服务开机关闭
[[email protected] Desktop]# systemctl disable sshd.service
Removed /etc/systemd/system/multi-user.target.wants/sshd.service.
[[email protected] Desktop]# systemctl status sshd

Linux下的进程管理

-systemctl set-default multi-user.target ##设定系统运行级别为无图形网络模式
[[email protected] Desktop]# systemctl set-default multi-user.target
Removed /etc/systemd/system/default.target.
Created symlink /etc/systemd/system/default.target → /usr/lib/systemd/system/multi-user.target.

Linux下的进程管理

-systemctl set-default graphical.target  ##设定系统运行级别为图形网络模式
[[email protected] Desktop]# systemctl set-default graphical.target
Removed /etc/systemd/system/default.target.
Created symlink /etc/systemd/system/default.target → /usr/lib/systemd/system/graphical.target.

Linux下的进程管理