linux进程管理

了解进程

进程定义

  • 已经启动的可执行的运行实例

组成

  • 已经分配内存的地址空间
  • 安全属性,包括所有权凭据和特权
  • 程序代码的一个或多个执行线程

程序和进程的区别

  • 程序:二进制文件
  • 进程:是程序运行时的过程,动态,有生命周期及运行状态

进程的生命周期

父进程复制自己的地址空间,创建一个新的子进程. 每个新进程分配一个唯一的进程ID(PID),满足跟踪安全性只需.  PID和父进程(PPID)是子进程环境的元素 . 任何进程都可以创建子进程

所有进程都是第一个系统进程的后代

  • centos5,6的PID为1的进程是init
  • centos7的PID为1的进程是systemd

僵尸进程

  • 父进程退出了,子进程没有退出.这些子进程没有父进程来管理,就变成了僵尸进程

查看进程

PS -aux

常用的参数

  • a: 显示跟当前终端关联的所有进程
  • u: 基于用户的格式显示
  • x: 显示所有进程,不以终端机来区分

[root@VM_0_15_centos ~]# ps -aux

USERPID%CPU%MEMVSZRSSTTYSTATSTARTTIMECOMMAND
oot10.00.31910083624?SsAug132:02/usr/lib/systemd/systemd --switched-ro
root20.00.000?SAug130:00[kthreadd]
root30.00.000?SAug130:31[ksoftirqd/0]
root50.00.000?S<Aug130:00[kworker/0:0H]
root70.00.000?SAug130:00[migration/0]
root80.00.000?SAug130:00[rcu_bh]
选项概述
user启动这些进程的用户
pid进程的id
%cpu进程占用cpu百分比
%men进程占用内存的百分比
vsz进程占用的虚拟内存大小(单位kb)
rss进程占用的物理内存大小(kb)
tty进程在哪个终端上运行
stat该进程目前的状态
start被触发启动的时间
time实际使用cpu运作的时间,有显示0:00的,耗费cpu资源不到1秒
command该程序的实际指令

stat选项详解

  • R:该程序目前正在运行,或者可被运行
  • S:该程序目前正在睡眠
  • T:该程序目前正在侦测或者停止了,
  • Z:该程序应该已经终止,但是其父进程无法正常的终止他,造成zombie僵尸程序的状态
  • D:不可中断状态|

五个基本状态后面还可以加一些字母

  • <:表示进程运行在高优先级上
  • N:表示进程运行在低优先级上
  • L:表示进程有页面锁定在内存中
  • s:表示进程是控制进程
  • l:表示进程是多线程的
  • +:表示当前进程运行在前台

tty选项详解

  • ?:表示与终端没有关系,大部分是内核的
  • tty1-tty6是本机上面的登入者程序,若为pts/0等等的,则表示由网络连接进主机的程序(是虚拟的)

ps -ef

  • -e: 显示所有进程
  • -f: 显示完整格式输出

[root@VM_0_15_centos ~]# ps -ef

UIDPIDPPIDCSTIMETTYTIMECMD
root100Aug13?00:02:02/usr/lib/systemd/systemd --switched-root --system --des
root200Aug13?00:00:00[kthreadd]
root320Aug13?00:00:31[ksoftirqd/0]
root520Aug13?00:00:00[kworker/0:0H]
root720Aug13?00:00:00[migration/0]
选项概述
UID启动这些进程的用户
PID进程的ID
PPID父进程的ID号
C进程生命周期的利用率
Stime启动启动时的系统时间
tty进程在哪个终端上运行
time运行进程累计需要的cpu时间
cmd启动的程序名称

uptime

查看系统负载

[root@VM_0_15_centos ~]# uptime
 19:36:10 up 13 days,  3:47,  1 user,  load average: 0.00, 0.01, 0.05
选项概述
19:36:10当前时间
up 13 days系统运行时间,说明这台服务器已经开机13天了
1 user当前登录的用户数
load average: 0.00, 0.01, 0.05cpu系统负载,即cpu任务队列的平均长度,三个数值分别是1分钟,5分钟,15分钟的平均值

互动:

找出当前系统中,cpu负载过高的服务器
服务器:load average 0.15, 0.08, 0.01         1核
服务器:load average 4.15, 6.08, 6.01        1核
服务器:load average 10.15, 10.08, 10.01    4核
 
答案:4核
经验:单核心,1分钟的系统平均负载不差过3,就可以,这是个经验值

top

[root@VM_0_15_centos ~]# top
top - 19:42:40 up 13 days,  3:54,  1 user,  load average: 0.00, 0.01, 0.05
Tasks:  74 total,   2 running,  72 sleeping,   0 stopped,   0 zombie
%Cpu(s):  1.0 us,  0.0 sy,  0.0 ni, 99.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  1014888 total,    65276 free,   108852 used,   840760 buff/cache
KiB Swap:        0 total,        0 free,        0 used.   733124 avail Mem 


  PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND                            
 3504 root      20   0  611428  13364   1972 S  0.7  1.3  64:16.54 barad_agent                        
14759 root      20   0  146564  10968   3112 S  0.3  1.1   1:14.98 YDService                          
    1 root      20   0  191008   3624   2284 S  0.0  0.4   2:02.70 systemd                            
    2 root      20   0       0      0      0 S  0.0  0.0   0:00.08 kthreadd                           
    3 root      20   0       0      0      0 S  0.0  0.0   0:31.08 ksoftirqd/0                        
    5 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 kworker/0:0H                       
    7 root      rt   0       0      0      0 S  0.0  0.0   0:00.00 migration/0    
  • 第一行,系统负载
  • 第二行,进程信息
    • tesks(当前任务数)
    • running 运行
    • sleeping 睡眠
    • stopped 终止
    • zombie 僵尸进程
  • 第三行cpu信息
    • us:用户占用cpu的时间
    • sy:内核占用cpu
    • ni:用户进程空间内改变过优先级占用的cpu
    • id:空闲cpu的百分比
    • wa:当对磁盘进行大量写时,发现IO等待会占用很多cpu,如果正常使用占用比较多cpu,磁盘性能有问题
    • hi:硬件中断占用的cpu,硬件驱动通常是内核中的一个子进程,而不是一个独立的进程
    • si:软中断,占用的cpu百分比,往磁盘写入数据有个队列,所占用的时间
    • st:虚拟机占用物理机cpu时间
  • 第四行内存信息
    • 单位kb
    • total 总内存
    • free 空闲
    • used 占用
    • buff/cache 这里的缓存是可以释放的
  • 第五行 swap
    • total 交换区总量
    • free 空闲的交换区总量
    • used 使用的交换区总量
    • avail Mem 总共的可利用内存是多少
    • 注意:如果swap分区,被使用,那么你的内存不够用了
选项概述
PID进程ID
USER进程所有者
PR优先级
NInice值
VIRT进程使用的虚拟内存总量
RED进程使用的物理内存总量
SHR共享内存大小
S进程状态
%CPU进程占用的CPU百分比
%MEM进程占用的物理内存百分比
TIME+进程使用的CPU总时间
COMMAND命令行

快捷键

  • 默认 3s 刷新一次
  • 按s修改刷新时间
  • 按空格立即刷新
  • 按大写的 C 按照cpu占用排序
  • 按大写的 M 安装内存占用排序
  • 按数字键1,显示每个内核的cpu使用频率
  • 按u: 显示指定的用户
  • h:帮助
  • q:退出

例:-p通过进程ID查看单个进程

[root@VM_0_15_centos ~]# top -p 3081

 PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND                                
 3081 root      20   0  112864   4304   3284 S  0.0  0.4   0:59.84 sshd    

例:找出系统中占用系统进程最多的进程

top命令执行后按大写的P

[root@VM_0_15_centos ~]# top
PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND                                
 2438 polkitd   20   0  614312  10460   1992 S  0.2  1.0   0:30.25 polkitd                                
 3504 root      20   0  611428  13364   1972 S  0.2  1.3  64:21.77 barad_agent                            
14759 root      20   0  146876  11204   3112 S  0.2  1.1   1:18.95 YDService 

在linux系统中最多可以使用100%CPU吗

理论上:如果你是4核心的cpu,你可以运行400%

lsof命令

选项

  • -p 进程号,列出指定进程号打开的文件
  • -i 条件:列出符合条件的进程

查看某个进程读了那些文件,调用了那些东西呢,一般用于木马进程

[root@VM_0_15_centos ~]# ps aux | grep ssh | grep -v grep
root       448  0.0  0.5 156744  5440 ?        Ss   16:43   0:00 sshd: root@pts/0
root      3081  0.0  0.4 112864  4304 ?        Ss   Aug13   1:02 /usr/sbin/sshd -D
root      8557  0.0  0.5 156624  5120 ?        Ss   17:53   0:00 sshd: unknown [priv]
sshd      8558  0.3  0.2 112864  2224 ?        S    17:53   0:00 sshd: unknown [net]
[root@VM_0_15_centos ~]# lsof -p 3081
COMMAND  PID USER   FD   TYPE             DEVICE SIZE/OFF     NODE NAME
sshd    3081 root  cwd    DIR              253,1     4096        2 /
sshd    3081 root  rtd    DIR              253,1     4096        2 /
sshd    3081 root  txt    REG              253,1   853040    16204 /usr/sbin/sshd
sshd    3081 root  mem    REG              253,1    61624    22098 /usr/lib64/libnss_files-2.17.so
...

查看端口,或者看黑客开启的后门端口是哪个进程在使用

[root@VM_0_15_centos ~]# lsof -i:22
COMMAND  PID USER   FD   TYPE   DEVICE SIZE/OFF NODE NAME
sshd     448 root    3u  IPv4 13695793      0t0  TCP VM_0_15_centos:ssh->144.0.8.26:9545 (ESTABLISHED)
sshd    3081 root    3u  IPv4    22003      0t0  TCP *:ssh (LISTEN)

pstree

以树形图显示进程,只显示进程的名字,且相同进程合并显示

[root@VM_0_15_centos ~]# pstree
systemd─┬─YDLive───{YDLive}
        ├─YDService───11*[{YDService}]
        ├─acpid
        ├─2*[agetty]
        ├─atd
        ├─auditd───{auditd}
        ├─barad_agent─┬─barad_agent
        │             └─barad_agent───3*[{barad_agent}]
        ├─crond
        ├─dbus-daemon
        ├─dhclient
        ├─lsmd
        ├─lvmetad
        ├─ntpd
        ├─polkitd───6*[{polkitd}]
        ├─rsyslogd───2*[{rsyslogd}]
        ├─sgagent───{sgagent}
        ├─sshd─┬─sshd───bash───pstree
        │      └─sshd───sshd
        ├─systemd-journal
        ├─systemd-logind
        ├─systemd-udevd
        └─tuned───4*[{tuned}]

pstree -p 进程ID

[root@VM_0_15_centos ~]# pstree -p 3081
sshd(3081)───sshd(448)───bash(493)───pstree(9591)

# 管理进程

通过信号来管理进程

列出所有信号

[root@VM_0_15_centos ~]# 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    

常用的信号

信号编号信号名解释
1SIGHUP重新加载配置
2SIGINT键盘中断ctrl+c
3SIGQUIT退出
9SIGKILL强制退出
15SIGTERM终止(正常结束),缺省信号
18SIGCONT继续
19SIGSTOP停止
20SIGTSTP暂停 ctrl+z

关闭进程

  • 1,kill [信号] [进程ID]
[root@VM_0_15_centos ~]# ps aux | grep top | grep -v grep
root     10472  0.0  0.2 159780  2188 pts/1    S+   18:10   0:00 top
[root@VM_0_15_centos ~]# kill 3 10472
[root@VM_0_15_centos ~]# ps aux | grep top | grep -v grep
  • 2,killall 和 pkill 命令通过指定名字来杀死进程

killall

[root@VM_0_15_centos ~]# ps aux | grep top | grep -v grep
root     11480  0.0  0.2 159780  2148 pts/1    S+   18:18   0:00 top
[root@VM_0_15_centos ~]# killall top
[root@VM_0_15_centos ~]# ps aux | grep top | grep -v grep
[root@VM_0_15_centos ~]# 

pkill

[root@VM_0_15_centos ~]# ps aux | grep top | grep -v grep
root     11676  0.0  0.2 159780  2144 pts/1    S+   18:20   0:00 top
[root@VM_0_15_centos ~]# pkill top
[root@VM_0_15_centos ~]# ps aux | grep top | grep -v grep
[root@VM_0_15_centos ~]# 

进程优先级管理

优先级的取值范围(-20,19),越小优先级越高,默认优先级是0

设置优先级,通过ctrl+z挂起进程

[root@VM_0_15_centos ~]# nice -n 5 vim h.txt

[1]+  Stopped                 nice -n 5 vim h.txt

查看进程的PID

[root@VM_0_15_centos ~]# ps aux | grep vim | grep -v grep
root     12206  0.0  0.4 151272  4944 pts/0    TN   18:24   0:00 vim h.txt

根据PID查看进程的信息

[root@VM_0_15_centos ~]# top -p 12206
  PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND                                
12206 root      25   5  151272   4944   2584 T  0.0  0.5   0:00.02 vim  

可以发现优先级是5

前台后台进程切换

&用在一个命令的最后,可以把这个命令放到后台执行

[root@VM_0_15_centos ~]# top &
[2] 13061

 ctrl + z可以将一个正在前台执行的程序放到后台

[root@VM_0_15_centos ~]# ll > ./123.txt
[root@VM_0_15_centos ~]# tail -f ./123.txt 
-rw-r--r-- 1 root root      136 Aug 24 23:25 2.txt
-rw-r--r-- 1 root root       49 Aug 24 23:23 a.txt
-rw-r--r-- 1 root root       50 Aug 24 14:12 b.txt
-rw-r--r-- 1 root root       12 Aug 24 23:05 cpu.txt
-rw-r--r-- 1 root root 52428800 Aug 24 23:16 c.txt
drwxr-xr-x 2 root root     4096 Aug 21 21:22 DES_DIR
-rw-r--r-- 1 root root       97 Aug 24 23:07 eof.sh
-rw-r--r-- 1 root root      275 Aug 21 21:38 packups.sh
-rw-r--r-- 1 root root      146 Aug 21 21:47 ping.sh
-rw-r--r-- 1 root root      292 Aug 21 21:59 useradd1.sh
^Z
[4]+  Stopped                 tail -f ./123.txt

jobs查看当前有多少在后台运行的进程,这是一个作业控制命令

[root@VM_0_15_centos ~]# jobs
[1]   Stopped                 nice -n 5 vim h.txt
[2]   Stopped                 top
[3]-  Stopped                 top
[4]+  Stopped                 tail -f ./123.txt

 

fg命令将后台中的命令调到前台继续运行,

如果后台中有多个命令可以用 fg %jobnumber将选中的命令调出,
%jobnumber是通过jobs命令查到的后台正在执行的命令的序号(不是pid) 

[root@VM_0_15_centos ~]# fg 4
tail -f ./123.txt

我们再次远程,进行更改文件

[root@VM_0_15_centos ~]# ls >> 123.txt 

发现在监视改文件

[root@VM_0_15_centos ~]# fg 4
tail -f ./123.txt
123.txt
1.txt
2.txt
a.txt
b.txt
cpu.txt
c.txt
DES_DIR
eof.sh
packups.sh
ping.sh
useradd1.sh

bg命令将一个在后台中暂停的进程,变成继续执行,用法同上

实战:使用screen后台实行执行命令备份命令

场景:公司晚上需要备份1t数据,我再xshell上直接执行备份脚本back.sh可以吗?
或直接运行back.sh&放到后台运行可以吗?
当关了xshell,back.sh&还在后台执行吗?

xshell长时间连接,如果本地网络偶尔断开或者xshell不小心关闭,都会让后台运行的备份命令停止运行的

  • 正确做法使用:screen

安装

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

 

用法:
执行screen,将打开一个全新的会话
运行你的备份任务,或者vim a.txt命令
这时可以键入快捷键ctrl+a+d  
就已经分离出一个单独的会话

[root@VM_0_15_centos ~]# screen
[detached from 15042.pts-0.VM_0_15_centos]

screen -ls 查看已经建立的会话id

[root@VM_0_15_centos ~]# screen -ls
There is a screen on:
    15042.pts-0.VM_0_15_centos  (Detached)
1 Socket in /var/run/screen/S-root.

[root@VM_0_15_centos ~]# 

通过PID 重新连接会话

[root@VM_0_15_centos ~]# screen -r 15042

创建会话时也可以指定名称

[root@VM_0_15_centos ~]# screen -S text

当不想使用screen会话了,exit退出

[root@VM_0_15_centos ~]# exit

转载于:https://www.cnblogs.com/inmeditation/p/11572317.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值