zoukankan      html  css  js  c++  java
  • Linux基础系列-Day4

    top系统监控

    top命令可以实时动态地查看系统的整体运行情况,是一个综合了多方信息监测系统性能和运行信息的实用工具。通过top命令所提供的互动式界面,用热键可以管理。

    [root@centos-python ~]# top
    top - 22:33:12 up 9 min,  2 users,  load average: 0.00, 0.07, 0.09
    Tasks: 366 total,   2 running, 364 sleeping,   0 stopped,   0 zombie
    %Cpu(s):  0.0 us,  0.3 sy,  0.0 ni, 99.3 id,  0.0 wa,  0.0 hi,  0.3 si,  0.0 st
    KiB Mem:   2035624 total,   333488 used,  1702136 free,      900 buffers
    KiB Swap:  2113532 total,        0 used,  2113532 free.   110984 cached Mem
    
       PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND 
    ...
    ...

    头部信息字段解释:

    • top - 22:33:12    当前系统时间
    • up 9 min       系统已经运行了9分钟
    • 2 user        当前有2个用户登录
    • load average: 0.00, 0.07, 0.09  CPU负载,即单位时间内处理的任务数量
      • 第一个浮点数表示1分钟内的平均负载
      • 第二个浮点数表示5分钟内的平均负载
      • 第三个浮点数表示15分钟内的平均负载
    • Tasks: 366 total      总进程数
    • 2 running      正在运行的进程数
    • 364 sleeping     睡眠的进程数,即等待中的进程
    • 0 stopped        停止的进程数
    • 0 zombie       冻结进程数,即僵尸进程,主进程已经结束,而子进程仍占用内存
    • Cpu(s)        CPU的利用率,CPU真正处在工作状态的时间
      • 0.0us  用户空间占用CPU百分比
      • 0.3sy  内核空间占用CPU百分比
      • 0.0ni  用户进程空间内改变过优先级的进程占用CPU百分比
      • 99.3id  空闲CPU百分比
      • 0.0wa  等待输入输出的CPU时间百分比
      • 0.0hi  硬中断占用CPU的百分比
      • 0.3si  软中断占用CPU的百分比
      • 0.0st  被偷走的CPU占用,当物理机内有虚拟机运行时候才会出现
    • KiB Mem:   内存信息(内存监控详细说明)
      • total  总共的内存
      • used  已使用内存
      • free  空闲内存
      • buffers  要往磁盘写但还没写进去的内存空间
      • cached Mem  存放从硬盘中调用的数据的内存空间
    • Swap:  交换空间信息
      • total  交换区总量
      • used  使用的交换区
      • free  空闲交换区总量

    free内存监控

    可以显示当前系统未使用的和已使用的内存数目,还可以显示被内核使用的内存缓冲区。

    内存:主要就是用来往外分buffers和cache

      buffers:指的是用来存放从内存中要往硬盘中写的数据但是还没有写入的内存空间,用来缓解内存和硬盘之间的速度差

      cached:指的是用来存放从硬盘中调用的数据的内存空间,CPU从这里面提取数据,用来缓解内存和CPU间的速度差

    命令说明(常用):

      -h  以可读性较高的方式来显示信息

      -b  以Byte为单位显示内存使用情况

      -k  以KB为单位显示内存使用情况

      -m  以MB为单位显示内存使用情况

      -s<间隔秒数>  持续观察内存使用状况

    CentOS7.0版本

    [root@centos-python ~]# free -h
                 total       used       free     shared    buffers     cached
    Mem:          1.9G       324M       1.6G       8.8M       900K       108M
    -/+ buffers/cache:       215M       1.7G
    Swap:         2.0G         0B       2.0G

    字段解释:

    • total      总量
    • used      使用量
    • free      剩余量
    • shared      共享内存,一般系统不会用到,等于是废弃的内存
    • buffers      空闲的buffers空间
    • cached      空闲的cached空间
    • -/+ buffers/cache:    使用和未使用的buffers/cache
      • (-buffers/cache) used内存数:第一部分Mem行中的 used – buffers – cached
      • (+buffers/cache) free内存数: 第一部分Mem行中的 free + buffers + cached

    清理缓存命令

    echo 3 > /proc/sys/vm/drop_caches    表示向drop_caches传送3号指令,清理缓存

    df查看磁盘

    用于显示磁盘分区上的可使用的磁盘空间。默认显示单位为KB。可以利用该命令来获取硬盘被占用了多少空间,目前还剩下多少空间等信息。

    命令说明(常用):

      -a  包含全部的文件系统

      -h  以可读性较高的方式来显示信息

      -i  显示inode的信息

      -l  仅显示本地端的文件系统

      -T  显示文件系统的类型

    [root@centos-python ~]# df -hT
    Filesystem              Type      Size  Used Avail Use% Mounted on
    /dev/mapper/centos-root xfs        50G  4.0G   46G   8% /
    devtmpfs                devtmpfs  985M     0  985M   0% /dev
    tmpfs                   tmpfs     994M     0  994M   0% /dev/shm
    tmpfs                   tmpfs     994M  8.9M  986M   1% /run
    tmpfs                   tmpfs     994M     0  994M   0% /sys/fs/cgroup
    /dev/sr0                iso9660   6.6G  6.6G     0 100% /mnt
    /dev/mapper/centos-home xfs        48G   36M   48G   1% /home
    /dev/sda1               xfs       497M  119M  379M  24% /boot

    字段解释:

    • Filesystem   磁盘设备
    • Type      文件系统类型
    • Size      总量
    • Used      使用量
    • Avail      剩余量
    • Use %      使用百分比
    • Mounted on    挂载点

    文件系统:操作系统在存储设备上组织文件和数据的方法

    常见文件系统类型

      Windows:NTFS、fat16、fat32等

      Linux:ext2、ext3、ext4、xfs、vfat等

    查看文件inodes

    [root@centos-python ~]# df -ih
    Filesystem              Inodes IUsed IFree IUse% Mounted on
    /dev/mapper/centos-root    50M  145K   50M    1% /
    devtmpfs                  247K   389  246K    1% /dev
    tmpfs                     249K     1  249K    1% /dev/shm
    tmpfs                     249K   494  249K    1% /run
    tmpfs                     249K    13  249K    1% /sys/fs/cgroup
    /dev/sr0                     0     0     0     - /mnt
    /dev/mapper/centos-home    48M   137   48M    1% /home
    /dev/sda1                 500K   330  500K    1% /boot

    对于服务器而言,很有可能出现存储大量小文件的情况,这种情况会造成磁盘空间未用完,但inodes却用完了,导致磁盘无法写入新数据。

    ps进程管理

    用于报告当前系统的进程状态。可以搭配kill指令随时中断、删除不必要的程序。ps命令是最基本同时也是非常强大的进程查看命令,使用该命令可以确定有哪些进程正在运行和运行的状态、进程是否结束、进程有没有僵死、哪些进程占用了过多的资源等等,总之大部分信息都是可以通过执行该命令得到的。

    命令说明(常用):

      a:显示现行终端机下的所有程序,包括其他用户的程序。

      u:以用户为主的格式来显示程序状况。

      x:显示所有程序,不以终端机来区分。

      e:列出程序时,显示每个程序所使用的环境变量。

      l:采用详细的格式来显示程序状况。

      f:用ASCII字符显示树状结构,表达程序间的相互关系。

      注:常用的两种组合aux,elf

    ps -aux查看进程信息

    [root@centos-python ~]# ps -aux |head -5
    USER        PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
    root          1  0.0  0.3  52840  6752 ?        Ss   May25   0:03 /usr/lib/systemd/systemd --switched-root --system --deserialize 23
    root          2  0.0  0.0      0     0 ?        S    May25   0:00 [kthreadd]
    root          3  0.0  0.0      0     0 ?        S    May25   0:03 [ksoftirqd/0]
    root          5  0.0  0.0      0     0 ?        S<   May25   0:00 [kworker/0:0H]
    ...
    ...

    字段解释:

    • USER    用户
    • PID     进程号
    • %CPU    CPU使用百分比
    • %MEM    内存使用百分比
    • VSZ      虚拟内存
    • RSS      实际内存占用
    • TTY     所在终端 
    • STAT    进程状态
    • START    进程启动时间
    • TIME    CPU累计使用时间
    • COMMAND    启动进程的命令

    ps -elf查看其他信息

    [root@centos-python ~]# ps -elf |head -5
    F S UID         PID   PPID  C PRI  NI ADDR SZ WCHAN  STIME TTY          TIME CMD
    4 S root          1      0  0  80   0 - 13210 ep_pol May25 ?        00:00:03 /usr/lib/systemd/systemd --switched-root --system --deserialize 23
    1 S root          2      0  0  80   0 -     0 kthrea May25 ?        00:00:00 [kthreadd]
    1 S root          3      2  0  80   0 -     0 smpboo May25 ?        00:00:03 [ksoftirqd/0]
    1 S root          5      2  0  60 -20 -     0 worker May25 ?        00:00:00 [kworker/0:0H]
    ....
    ....

    主要是查看父进程PPID号

    kill和pkill杀死进程

    kill/pkill

    用来删除执行中的程序或工作。kill可将指定的信息送至程序。预设的信息为SIGTERM(15),可将指定程序终止。若仍无法终止该程序,可使用SIGKILL(9)信息尝试强制删除程序。

    一般使用-9指令强制杀死进程

    命令格式:kill命令后面跟进程pid,pkill后面跟进程名字

      kill -9 pid

      pkill -9 process_name

    pstree查看进程树

    以树状图的方式展现进程之间的派生关系,显示效果比较直观。
    命令说明:pstree process_name

    查看系统所有进程

    [root@centos-python ~]# pstree
    systemd─┬─ModemManager───2*[{ModemManager}]
            ├─NetworkManager─┬─dhclient
            │                └─3*[{NetworkManager}]
            ├─2*[abrt-watch-log]
            ├─abrtd
            ├─alsactl
            ├─atd
            ├─auditd─┬─audispd─┬─sedispatch
            │        │         └─{audispd}
            │        └─{auditd}
            ├─avahi-daemon───avahi-daemon
            ├─chronyd
            ├─crond
            ├─dbus-daemon───{dbus-daemon}
            ├─firewalld───{firewalld}
            ├─iprdump
            ├─iprinit
            ├─iprupdate
            ├─ksmtuned───sleep
            ├─libvirtd───10*[{libvirtd}]
            ├─login───bash
            ├─lsmd
            ├─lvmetad
            ├─master─┬─pickup
            │        └─qmgr
            ├─polkitd───5*[{polkitd}]
            ├─rpc.statd
            ├─rpcbind
            ├─rsyslogd───2*[{rsyslogd}]
            ├─smartd
            ├─sshd───sshd───bash───pstree
            ├─systemd-journal
            ├─systemd-logind
            ├─systemd-udevd
            ├─tuned───4*[{tuned}]
            └─vmtoolsd───{vmtoolsd}

    fdisk硬盘分区

    用于观察硬盘实体使用情况,也可对硬盘分区,它采用的是传统的问答式界面
    命令说明:
      -l    列出当前系统内磁盘信息

    硬盘分区示例:

    查看当前系统内磁盘信息

    [root@centos-python ~]# fdisk -l
    
    Disk /dev/sda: 107.4 GB, 107374182400 bytes, 209715200 sectors
    Units = sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
    Disk label type: dos
    Disk identifier: 0x000bda30
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/sda1   *        2048     1026047      512000   83  Linux
    /dev/sda2         1026048   209715199   104344576   8e  Linux LVM
    
    Disk /dev/sdb: 10.7 GB, 10737418240 bytes, 20971520 sectors
    Units = sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
    
    
    Disk /dev/mapper/centos-swap: 2164 MB, 2164260864 bytes, 4227072 sectors
    Units = sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
    
    
    Disk /dev/mapper/centos-root: 53.7 GB, 53687091200 bytes, 104857600 sectors
    Units = sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
    
    
    Disk /dev/mapper/centos-home: 51.0 GB, 50994348032 bytes, 99598336 sectors
    Units = sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes

    在裸盘sdb上创建分区

    [root@centos-python ~]# fdisk /dev/sdb 
    Welcome to fdisk (util-linux 2.23.2).
    
    Changes will remain in memory only, until you decide to write them.
    Be careful before using the write command.
    
    Device does not contain a recognized partition table
    Building a new DOS disklabel with disk identifier 0x8b08335d.
    
    Command (m for help): 

    按m可查看帮助,创建一个新磁盘按n

    Command (m for help): n              #创建一个新分区
    Partition type:
       p   primary (0 primary, 0 extended, 4 free)
       e   extended
    Select (default p): p                #创建一个主分区,e为扩展分区
    Partition number (1-4, default 1):         #指定一个分区编号,默认1
    First sector (2048-20971519, default 2048):     #指定起始扇区,默认2048开始
    Using default value 2048
    Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519): +1G      #指定空间大小为1G
    Partition 1 of type Linux and of size 1 GiB is set
    
    Command (m for help): w            #保存分区信息并退出
    The partition table has been altered!
    
    Calling ioctl() to re-read partition table.
    Syncing disks.
    [root@centos-python ~]# fdisk -l /dev/sdb 
    
    Disk /dev/sdb: 10.7 GB, 10737418240 bytes, 20971520 sectors
    Units = sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
    Disk label type: dos
    Disk identifier: 0x2067e569
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/sdb1            2048     2099199     1048576   83  Linux

    创建分区后,需要创建文件系统,并挂在到目录上才能使用,一个分区可以挂载到多个目录上

    [root@centos-python ~]# mkfs.xfs /dev/sdb1               #格式化为xfs文件系统,输入mkfs.后按两下tab可以显示出当前系统支持的文件系统类型
    meta-data=/dev/sdb1              isize=256    agcount=4, agsize=65536 blks
             =                       sectsz=512   attr=2, projid32bit=1
             =                       crc=0
    data     =                       bsize=4096   blocks=262144, imaxpct=25
             =                       sunit=0      swidth=0 blks
    naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
    log      =internal log           bsize=4096   blocks=2560, version=2
             =                       sectsz=512   sunit=0 blks, lazy-count=1
    realtime =none                   extsz=4096   blocks=0, rtextents=0
    [root@centos-python ~]# mkdir /sdb1
    [root@centos-python ~]# mount /dev/sdb1 /sdb1          #挂载sdb1分区到sdb1目录上,可挂载到多个目录上,挂载后即可使用
    [root@centos-python ~]# df -hT |grep sdb1
    /dev/sdb1               xfs      1014M   33M  982M   4% /sdb1

    创建一个新分区,配置成swap使用

    [root@centos-python ~]# fdisk /dev/sdb 
    Welcome to fdisk (util-linux 2.23.2).
    
    Changes will remain in memory only, until you decide to write them.
    Be careful before using the write command.
    
    
    Command (m for help): n
    Partition type:
       p   primary (1 primary, 0 extended, 3 free)
       e   extended
    Select (default p): e                    #创建扩展分区
    Partition number (2-4, default 2): 
    First sector (2099200-20971519, default 2099200): 
    Using default value 2099200
    Last sector, +sectors or +size{K,M,G} (2099200-20971519, default 20971519): 
    Using default value 20971519
    Partition 2 of type Extended and of size 9 GiB is set
    
    Command (m for help): n
    Partition type:
       p   primary (1 primary, 1 extended, 2 free)
       l   logical (numbered from 5)            #逻辑分区,如果创建了三个主分区一个扩展分区,就没有p选项了
    Select (default p): l                  #选择逻辑分区
    Adding logical partition 5
    First sector (2101248-20971519, default 2101248): 
    Using default value 2101248
    Last sector, +sectors or +size{K,M,G} (2101248-20971519, default 20971519): +2G
    Partition 5 of type Linux and of size 2 GiB is set
    
    Command (m for help): w
    The partition table has been altered!
    
    Calling ioctl() to re-read partition table.
    Syncing disks.

    将新分区格式化成swap,并激活使用

    [root@centos-python ~]# mkswap /dev/sdb5               #格式化成swap分区
    Setting up swapspace version 1, size = 2097148 KiB
    no label, UUID=6056a0ac-5d62-4f57-9243-03eed594e73a
    [root@centos-python ~]# swapon /dev/sdb5               #激活swap分区使用
    [root@centos-python ~]# swapon -s                   #查看当前系统内的swap分区使用情况
    Filename                                Type            Size    Used    Priority
    /dev/dm-0                               partition       2113532 0       -1
    /dev/sdb5                               partition       2097148 0       -2
    [root@centos-python ~]# swapoff /dev/sdb5               #关闭swap分区
    [root@centos-python ~]# swapon -s
    Filename                                Type            Size    Used    Priority
    /dev/dm-0                               partition       2113532 0       -1
  • 相关阅读:
    元素模式
    完美C++(第5版)(双色)
    ASP.NET Web API 2框架揭秘
    跨终端 Web
    WebKit技术内幕
    云计算时代——本质、技术、创新、战略
    互联网创业密码
    Netty权威指南
    Robot Framework 学习笔记(二)-------第一个脚本
    Robot Framework学习笔记(一)------环境搭建
  • 原文地址:https://www.cnblogs.com/zero527/p/6907281.html
Copyright © 2011-2022 走看看