> For the complete documentation index, see [llms.txt](https://lack.gitbook.io/operation-and-maintenance/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://lack.gitbook.io/operation-and-maintenance/yun-ji-suan.md).

# 日常运维

## w 命令

```bash
[root@localhost ~]# w
 19:37:54 up 44 min,  1 user,  load average: 0.00, 0.01, 0.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    192.168.127.1    18:55    2.00s  0.08s  0.00s w
```

`w` 命令 :

`18:28:13 up 2 min, 1 user, load average: 0.05, 0.05, 0.01` (同uptime)

```bash
[root@localhost ~]# uptime
 19:07:57 up 14 min,  1 user,  load average: 0.00, 0.02, 0.05
```

`load average: 0.05, 0.05, 0.01` ：1分钟内，5分钟内，15分钟内使用和等待CPU的进程数\
，一般数值小于CPU核数为正常

`18:28:13 up 2 min` ：运行时间

`1 user` ：用户数

用户名 终端 登录地址 登录时间 空闲时间 终端下进程和子进程占用CPU的时间 命令执行时间 命令

1.USER 2.TTY 3.FROM 4.LOGIN@ 5.IDLE 6.JCPU 7.PCPU 8.WHAT

root pts/0 192.168.130.1 18:26 0.00s 0.02s 0.00s w

```bash
[root@localhost ~]# cat /proc/cpuinfo  # 查看系统cpu信息
processor       : 0 # 为逻辑CPU个数减一
vendor_id       : GenuineIntel
cpu family      : 6
model           : 58
model name      : Intel(R) Core(TM) i5-3230M CPU @ 2.60GHz
stepping        : 9
microcode       : 0x1c
cpu MHz         : 2601.000
cache size      : 3072 KB
physical id     : 0
siblings        : 1
core id         : 0
cpu cores       : 1
apicid          : 0
initial apicid  : 0
fpu             : yes
fpu_exception   : yes
cpuid level     : 13
wp              : yes
flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss syscall nx rdtscp lm constant_tsc arch_perfmon nopl xtopology tsc_reliable nonstop_tsc pni pclmulqdq vmx ssse3 cx16 pcid sse4_1 sse4_2 x2apic popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm tpr_shadow vnmi ept vpid fsgsbase tsc_adjust smep arat
bogomips        : 5202.00
clflush size    : 64
cache_alignment : 64
address sizes   : 43 bits physical, 48 bits virtual
power management:
```

## vmstat 命令

`vmstat` 命令 ：

介绍：动态显示系统负载状态

格式：vmstat \[option]

* vmstat N M ：每Ns显示一次，共显示M次

```bash
[root@localhost ~]# vmstat 1 2
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 2  0      0 478328   2076 376100    0    0   144    94  111  176  1  1 96  2  0
 0  0      0 478328   2076 376100    0    0     0     0   51   46  0  0 100  0  0
```

vmstat 结果说明：

procs 进程：

* r (run) ：每N时间内的任务数
* b (block) ：被I/O堵塞的任务数

memory内存：

* swpd ：交换分区的数据量kb(数值大且频繁变动是当注意)
* free ：内存剩余数
* buff：缓冲大小
* cache：缓存大小

swap 交换分区：

* si (storage in)：写入内存
* so (storage out)：从内存读出的

> si so 频繁变动且大于零时说明内存不足

io 磁盘：

* bi (block in)：读磁盘
* bo (block out)：写磁盘

> 普通硬盘长期大于1000时有问题

system 显示采集间隔内发生的中断次数:

* in ：表示在某一时间间隔中观测到的每秒设备中断数
* cs ：上下文切换数

cpu：

* us ：用户占用百分比&#x20;
* sy ：系统占用百分比
* id ：空闲百分比
* wa ：等待百分比
* st ：被偷百分比

## top 命令

`top` ：动态查看负载

```bash
Tasks:  92 total,   1 running,  91 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.0 us,  0.3 sy,  0.0 ni, 99.7 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :   997956 total,   478088 free,   141684 used,   378184 buff/cache
KiB Swap:  2097148 total,  2097148 free,        0 used.   671184 avail Mem 

   PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND                                                        
  2231 root      20   0  161944   2212   1556 R  0.7  0.2   0:00.02 top                                                            
     1 root      20   0  125328   3848   2592 S  0.0  0.4   0:01.57 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.10 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
```

* PID ：进程ID号 可在/proc/ 下查看
* USER : 用户名 shirf + u查看
* PR ： 0-39 越高优先级越高
* NT ：-20-19

> PR+NT <=20

* VIRI ：虚拟内存
* RES ：物理内存
* SHR ：共享内存
* TIME+ ：运行时长
* %CPU ：占用CPU百分比 shift + t
* %MEM ：占用内存百分比 shift + m

用法：

* -bn 1 ：一次性显示(可用于脚本)
* -d # ：指定间隔时间
* -c ：详细显示最后一列

## sar 命令

`sar` 命令：

介绍：显示网卡流量，需要`yum install -y sysstat`

格式：sar -n DEV 1 10 :显示网卡流量 每秒1次，共10次

用法：

* -q : 历史负载
* -b ：磁盘
* -p ：cpu
* -f filename  : 查看某一天网卡历史记录（/var/log/sa/下)
* -q  ：查看历史负载

```bash
[root@localhost ~]# sar -n DEV 1 3
Linux 3.10.0-862.el7.x86_64 (localhost.localdomain)     2018年08月05日  _x86_64_        (1 CPU)

19时44分21秒     IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s
19时44分22秒        lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00
19时44分22秒     ens33      0.99      0.99      0.06      0.18      0.00      0.00      0.00

19时44分22秒     IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s
19时44分23秒        lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00
19时44分23秒     ens33      1.00      1.00      0.06      0.38      0.00      0.00      0.00

19时44分23秒     IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s
19时44分24秒        lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00
19时44分24秒     ens33      0.99      0.99      0.06      0.38      0.00      0.00      0.00

平均时间:     IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s
平均时间:        lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00
平均时间:     ens33      0.99      0.99      0.06      0.31      0.00      0.00      0.00
[root@localhost ~]# sar -q
Linux 3.10.0-862.el7.x86_64 (localhost.localdomain)     2018年08月05日  _x86_64_        (1 CPU)

19时20分01秒   runq-sz  plist-sz   ldavg-1   ldavg-5  ldavg-15   blocked
19时30分01秒         1       109      0.00      0.01      0.05         0
19时40分01秒         1       109      0.00      0.01      0.05         0
平均时间:         1       109      0.00      0.01      0.05         0
[root@localhost ~]# sar -b
Linux 3.10.0-862.el7.x86_64 (localhost.localdomain)     2018年08月05日  _x86_64_        (1 CPU)

19时20分01秒       tps      rtps      wtps   bread/s   bwrtn/s
19时30分01秒      0.05      0.00      0.05      0.00      0.59
19时40分01秒      0.03      0.00      0.03      0.00      0.28
平均时间:      0.04      0.00      0.04      0.00      0.43
[root@localhost ~]# sar -p
Linux 3.10.0-862.el7.x86_64 (localhost.localdomain)     2018年08月05日  _x86_64_        (1 CPU)

19时20分01秒     CPU     %user     %nice   %system   %iowait    %steal     %idle
19时30分01秒     all      0.03      0.00      0.10      0.00      0.00     99.87
19时40分01秒     all      0.02      0.00      0.09      0.00      0.00     99.88
平均时间:     all      0.03      0.00      0.10      0.00      0.00     99.87
```

> 还有一个命令 nload，需要安装nload

## free 命令

`free` 命令：

介绍：输出linux内存信息

格式：free \[option]

用法：

* -m ：M为单位，默认为kb
* -g ：G为单位
* -h ：合适的单位

```bash
[root@localhost ~]# free -h
              total        used        free      shared  buff/cache   available
Mem:           974M        138M        463M        7.6M        372M        655M
Swap:          2.0G          0B        2.0G
```

> 内存公式：total = used + free + shared + buff(缓冲)/cache(缓存)
>
> available包含buff和cache剩余部分

## ps 命令

ps 命令

介绍：查看进程

格式：ps \[option]

用法：

* ps aux
* ps -elf

```bash
[root@localhost ~]# ps aux
USER        PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root          1  0.0  0.3 125328  3848 ?        Ss   18:53   0:01 /usr/lib/systemd/systemd --switched-root --system --deserialize 2
root          2  0.0  0.0      0     0 ?        S    18:53   0:00 [kthreadd]
root          3  0.0  0.0      0     0 ?        S    18:53   0:00 [ksoftirqd/0]
root          5  0.0  0.0      0     0 ?        S<   18:53   0:00 [kworker/0:0H]
root          7  0.0  0.0      0     0 ?        S    18:53   0:00 [migration/0]
root          8  0.0  0.0      0     0 ?        S    18:53   0:00 [rcu_bh]
root          9  0.0  0.0      0     0 ?        R    18:53   0:00 [rcu_sched]
root         10  0.0  0.0      0     0 ?        S<   18:53   0:00 [lru-add-drain]
root         11  0.0  0.0      0     0 ?        S    18:53   0:00 [watchdog/0]
root         13  0.0  0.0      0     0 ?        S    18:53   0:00 [kdevtmpfs]
root         14  0.0  0.0      0     0 ?        S<   18:53   0:00 [netns]
root         15  0.0  0.0      0     0 ?        S    18:53   0:00 [khungtaskd]
root         16  0.0  0.0      0     0 ?        S<   18:53   0:00 [writeback]
root         17  0.0  0.0      0     0 ?        S<   18:53   0:00 [kintegrityd]
root         18  0.0  0.0      0     0 ?        S<   18:53   0:00 [bioset]
root         19  0.0  0.0      0     0 ?        S<   18:53   0:00 [kblockd]
```

输出解析：

USER：进程用户

PID：进程ID

%CPU：使用CPU百分比

%MEM：使用内存百分比

VSZ：使用虚拟内存

RSS：使用物理内存

TTY：运行终端

STAT ：进程运行状态

* S ：sleep
* s ：主进程
* < : 高优先级
* N ：低优先级
* +：前台
* R ：running&#x20;
* L ：被锁
* l ：多线程进程
* Z ：僵尸进程
* X ：死亡进程
* T ：暂停进程
* D ：不可中断进程

TIME：时间

COMMEND：命令
