1.用top命令实时监测CPU、内存、硬盘状态

效果类似Windows的任务管理器,默认每5秒刷新一下屏幕上的显示结果。

[root@localhost /]# top

top - 17:39:47 up 45 min,  2 users,  load average: 0.07, 0.05, 0.05
Tasks: 175 total,   3 running, 172 sleeping,   0 stopped,   0 zombie
%Cpu(s): 14.0 us,  2.7 sy,  0.0 ni, 82.9 id,  0.0 wa,  0.0 hi,  0.3 si,  0.0 st
KiB Mem :  1875716 total,   883260 free,   511916 used,   480540 buff/cache
KiB Swap:  2097148 total,  2097148 free,        0 used.  1160536 avail Mem 

  PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND     
 2930 root      20   0 1491516 179012  49708 S  9.6  9.5   0:38.08 gnome-shell 
 1308 root      20   0  221852  29924  10696 R  5.0  1.6   0:12.65 Xorg        
 3442 root      20   0  573828  24200  15116 R  2.0  1.3   0:06.38 gnome-termi+
  670 root      20   0    4368    592    496 S  0.3  0.0   0:00.94 rngd        
  710 root      20   0  302644   6180   4804 S  0.3  0.3   0:04.39 vmtoolsd    
 2888 root      20   0  201268   3408   2756 S  0.3  0.2   0:00.29 at-spi2-reg+
 2900 root      20   0 1159540  23944  15760 S  0.3  1.3   0:00.95 gnome-setti+
 3044 root      20   0  377916  18660  14776 S  0.3  1.0   0:04.27 vmtoolsd    
 4276 root      20   0  157708   2256   1556 R  0.3  0.1   0:00.26 top         
    1 root      20   0  128092   6692   3944 S  0.0  0.4   0:02.37 systemd     
    2 root      20   0       0      0      0 S  0.0  0.0   0:00.00 kthreadd    
    3 root      20   0       0      0      0 S  0.0  0.0   0:00.16 ksoftirqd/0 
    6 root      20   0       0      0      0 S  0.0  0.0   0:00.18 kworker/u12+
    7 root      rt   0       0      0      0 S  0.0  0.0   0:00.00 migration/0 
    8 root      20   0       0      0      0 S  0.0  0.0   0:00.00 rcu_bh      
    9 root      20   0       0      0      0 S  0.0  0.0   0:00.77 rcu_sched   
   10 root      rt   0       0      0      0 S  0.0  0.0   0:00.03 watchdog/0  

1)第一行是任务队列信息

分别是当前时间,系统运行时间,当前登录用户数,系统负载(三个数值分别是1min.5min.15min内的平均值)

2)第二、三行为进程和CPU的信息

分别是进程总数,正在运行的进程数,睡眠的进程数,停止的进程数,僵尸进程数,用户控件占CPU的百分比,内核空间占CPU的百分比,用户进程空间内改变优先级的进程占CPU的百分比,空闲CPU的百分比,等待输入输出的CPU时间的百分比,硬件中断占用CPU时间的百分比,软件中断占用CPU时间的百分比

僵尸进程:是指如果一个进程结束后,如果他的父进程没有等待或调用他,那么他就变成了僵尸进程,仍然占用CPU

3)第四、五行为内存信息

4)下面是进程信息

  pid:进程ID

  user:进程所有者的用户名

  pr:优先级

  ni:nice 值,负值表示高优先级,正值表示低优先级

  virt:进程使用的虚拟内存总量,单位KB,VIRT=SWAP+RES

  res:进程使用的、未被换出的物理内存大小,单位KB,RES=CODE+DATA

  shr:共享内存大小,单位KB

  s:进程状态。(D=不可中断的睡眠状态,R=运行,S=睡眠,T=跟踪/停止,Z=僵尸进程)

  command:命令名/命令行

  cpu利用率与load average

  cpu是用来干活的,按照这个层面去理解,每个码农都是一个cpu

 

cpu利用率:上一天班的时间是8个小时,而码农敲代码的时间为2个小时,2/8=0.25  ,25%就是码农在一天的时间内的利用率(正常情况,cpu利用率<70%)

cpu负载:公司在一分钟内为某个码农安排了3个任务,而1分钟内该码农能做6个任务,那么该码农一分钟内的负载为0.5

如果公司在5分钟内为某个码农安排了100个任务,而5分钟内该码农只能做50个任务,那么该码农5分钟内的负载为2.0,即超负荷运转

cpu负载<=1:能正常应付

cpu负载>1:超负荷运转

如果有4核,相当于将100个任务交给4个码农去干,如果每个码农的负载都是100%,那么整体的cpu负载达到4.0则是很正常的现象

多核cpu, load average 应该 <= cpu核数 * 0.7


  存在1分钟,5分钟,15分钟三种cpu负载的原因是为了以一个更准确的方式来描述CPU的使用状态,如果仅是1分钟的CPU负载很高,5分钟或者15分钟后就降下来,那么就不用担心,如果CPU的使用率一直很高,那就需要提高硬件配置了。

在两种情况下会导致一个进程在逻辑上不能运行

1) 进程挂起是自身原因,遇到I/O阻塞,便要让出CPU让其他进程去执行,这样保证CPU一直在工作

2) 与进程无关,是操作系统层面,可能会因为一个进程占用时间过多,或者优先级等原因,而调用其他的进程去使用CPU。

 

2.free命令:检测系统已用和未用的内存空间总和

[root@localhost /]# free
              total        used        free      shared  buff/cache   available
Mem:        1875716      505684      890048       10752      479984     1166956
Swap:       2097148           0     2097148

使用free -w可以把buffer/cache拆开显示

使用free -h会以人类可读的方式显示

[root@localhost /]# free -h
              total        used        free      shared  buff/cache   available
Mem:           1.8G        517M        843M         10M        470M        1.1G
Swap:          2.0G          0B        2.0G

buffer和cache的解释:

A buffer is something that has yet to be "written" to disk. 

A cache is something that has been "read" from the disk and stored for later use.

也就是说buffer是用于存放要输出到disk(块设备)的数据的,而cache是存放从disk上读出的数据。这二者是为了提高IO性能的,并由OS管理。

像Windows这种成熟的系统,会多分配一些空间给cache以提高I/O的性能

available 表示cache能释放的内存+可作为suffer/cache的内存,直接就代表内存的剩余量

命令 while :; do free ; sleep 1 ; clear ;done可以实现动态监测

命令 echo 3 > /proc/sys/vm/drop_caches可以释放cache

3.进程的监测

用ps aux命令查看进程

用ps aux |head -n查看前n行

[root@localhost /]# ps aux |head -5
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.3 128092  6692 ?        Ss   16:54   0:02 /usr/lib/systemd/systemd --switched-root --system --deserialize 21
root         2  0.0  0.0      0     0 ?        S    16:54   0:00 [kthreadd]
root         3  0.0  0.0      0     0 ?        S    16:54   0:00 [ksoftirqd/0]
root         7  0.0  0.0      0     0 ?        S    16:54   0:00 [migration/0]

用ps aux | grep'top'可以过滤包含‘top’字符的内容

[root@localhost /]# ps aux |grep 'top'
root      3035  0.0  1.5 1020224 29872 ?       Sl   16:55   0:01 nautilus --no-default-window --force-desktop
root      7548  0.0  0.0 112664   968 pts/1    R+   18:54   0:00 grep --color=auto top

杀死一个进程用kill -9 PID命令

我们演示打开一个Firefox程序,然后用命令杀掉它,

[root@localhost /]# ps aux | grep 'firefox'
root      8384  7.6 10.1 2098636 190952 pts/1  Sl+  18:59   0:05 /usr/lib64/firefox/firefox
root      8762  0.0  0.0 112664   972 pts/2    R+   19:00   0:00 grep --color=auto firefox
[root@localhost /]# kill -9 8384

Linux基础(四)

我们还可以以树状图的方式查看进程,用命令 pstree PID

t@localhost /]# pstree 2
kthreadd─┬─ata_sff
         ├─3*[bioset]
         ├─crypto
         ├─deferwq
         ├─fsnotify_mark
         ├─2*[hci0]
         ├─ipv6_addrconf
         ├─kauditd
         ├─kblockd
         ├─kdevtmpfs
         ├─2*[kdmflush]
         ├─khugepaged
         ├─khungtaskd
         ├─kintegrityd
         ├─kmpath_rdacd
         ├─kpsmoused
         ├─krfcommd
         ├─ksmd
         ├─ksoftirqd/0
         ├─kswapd0
         ├─kthrotld
         ├─kworker/0:0
         ├─kworker/0:0H
         ├─kworker/0:1
         ├─kworker/0:1H
         ├─kworker/0:2
         ├─kworker/u128:1
         ├─kworker/u128:2
         ├─kworker/u129:0
         ├─kworker/u129:1
         ├─md
         ├─migration/0
         ├─mpt/0
         ├─mpt_poll_0
         ├─netns
         ├─nfit
         ├─rcu_bh
         ├─rcu_sched
         ├─rpciod
         ├─scsi_eh_0
         ├─scsi_eh_1
         ├─scsi_eh_2
         ├─scsi_tmf_0
         ├─scsi_tmf_1
         ├─scsi_tmf_2
         ├─ttm_swap
         ├─watchdog/0
         ├─writeback
         ├─xfs-buf/dm-0
         ├─xfs-buf/sda1
         ├─xfs-buf/sdb1
         ├─xfs-cil/dm-0
         ├─xfs-cil/sda1
         ├─xfs-cil/sdb1
         ├─xfs-conv/dm-0
         ├─xfs-conv/sda1
         ├─xfs-conv/sdb1
         ├─xfs-data/dm-0
         ├─xfs-data/sda1
         ├─xfs-data/sdb1
         ├─xfs-eofblocks/d
         ├─2*[xfs-eofblocks/s]
         ├─xfs-log/dm-0
         ├─xfs-log/sda1
         ├─xfs-log/sdb1
         ├─xfs-reclaim/dm-
         ├─xfs-reclaim/sda
         ├─xfs-reclaim/sdb
         ├─xfs_mru_cache
         ├─xfsaild/dm-0
         ├─xfsaild/sda1
         ├─xfsaild/sdb1
         └─xfsalloc
View Code

相关文章:

  • 2021-10-19
  • 2021-07-14
  • 2022-12-23
  • 2021-10-05
  • 2021-11-18
  • 2021-05-30
  • 2021-06-01
  • 2022-01-08
猜你喜欢
  • 2022-01-05
  • 2021-05-02
  • 2021-08-22
  • 2021-11-15
  • 2021-07-24
  • 2021-04-06
  • 2021-07-23
相关资源
相似解决方案