使用w命令查看当前系统的负载
[root@apenglinux-002 ~]# w 20:38:14 up 30 min, 2 users, load average: 0.00, 0.01, 0.05USER TTY FROM LOGIN@ IDLE JCPU PCPU WHATroot tty1 20:08 29:50 0.03s 0.03s -bashroot pts/0 192.168.221.1 20:08 6.00s 0.24s 0.01s w
第一行:时间、系统运行时间、登录用户、平均负载(1,5,15分钟内系统的平均负载),表示单位时间内使用cpu的活动进程数,值越大就说明服务器压力越大。一般情况下,这个值只要不超过cpu数量就没有关系
第二行以下项目显示顺序如下
登录帐号、终端名称、远程主机ip、登录时间、空闲时间、jcpu、pcpu、当前正在运行进程的命令行
jcpu 指的是与该tty终端连接的所有进程占用的时间,不包括过去的后台作业
pcpu 指的是当前进程所占用的时间
查看cpu的相关信息
[root@apenglinux-002 ~]# cat /proc/cpuinfo[root@apenglinux-002 ~]# grep -c 'processor' /proc/cpuinfo //查看当前系统有几个cpu(cpu有多少核)[root@apenglinux-002 ~]# cat /proc/cpuinfo |grep 'physical id' //所对应的值
vmstat命令监控系统的状态
[root@apenglinux-002 ~]# vmstat procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu----- r b swpd free buff cache si so bi bo in cs us sy id wa st 1 0 0 1512064 4136 225364 0 0 11 2 28 25 0 0 100 0 0
一 procs显示进程的相关信息
r 表示运行或等待cpu时间片的进程数
b 表示等待资源的进程数
二memory显示内存的相关信息
swpd 表示切换到交换分区中的内存数量,单位为KB
free 表示当前空闲的内存数量
buff 表示(写入磁盘)缓冲大小
cache 表示(从磁盘中读取的)缓存大小
三swap显示内存的交换情况
si 表示由交换区写入内存的数据量
so 表示由内存写入交换区的数据量
四io显示磁盘的使用情况
bi 表示块设备读取数据的量(读磁盘)
bo 表示从块设备写入数据的量(写磁盘)
五system显示采集间隔内发生的中断次数
in 表示在某一时间间隔内观测到的每秒设备的中断次数
cs 表示每秒产生的上下文切换次数
六cpu 显示cpu的使用状态
us 显示用户下所花费cpu的时间百分比
sy 显示系统花费cpu的时间百分比
id 显示cpu处于空闲状态的时间百分比
wa 表示I/O等待所占用cpu的时间百分比
st 表示被偷走的cpu所占百分比(一般都为0,不用关注)
[root@apenglinux-002 ~]# vmstat 1 5procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu----- r b swpd free buff cache si so bi bo in cs us sy id wa st 1 0 0 1512064 4136 225396 0 0 9 2 25 21 0 0 100 0 0 0 0 0 1512064 4136 225396 0 0 0 0 74 47 0 0 100 0 0 0 0 0 1512064 4136 225396 0 0 0 0 52 44 0 0 100 0 0 0 0 0 1512064 4136 225396 0 0 0 0 56 47 0 0 100 0 0 0 0 0 1512064 4136 225396 0 0 0 0 49 38 0 0 100 0 0
表示每隔1秒输出一次状态,共输出5次
[root@apenglinux-002 ~]# vmstat 1
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
1 0 0 1512188 4136 225396 0 0 9 2 24 21 0 0 100 0 0
0 0 0 1512188 4136 225396 0 0 0 0 82 53 0 0 100 0 0
0 0 0 1512188 4136 225396 0 0 0 0 70 52 0 0 100 0 0
0 0 0 1512188 4136 225396 0 0 0 0 55 45 0 0 100 0 0
0 0 0 1512188 4136 225396 0 0 0 0 47 40 0 0 100 0 0
...................................................................................................................................................
表示每隔1秒输出一次状态且一直输出,除非按ctrl+c键结束
用top命令显示进程所占的系统资源
top命令用于动态监控进程所占的系统资源,每隔3秒变一次。
shift+m键可以按照内存使用大小排序
数字1键可以列出所有核cpu的使用状态
q键可以退出top
[root@apenglinux-002 ~]# top -bn1 //非动态打印系统资源的使用情况,一次性输出所有信息# top -d 1 //1秒钟执行一次# top -d 1 -p 10126 //查看指定进程的动态信息# top -d 1 -p 10126,1# top -d 1 -u mysql //查看指定用户的进程# top -d 1 -b -n 2 > top.txt //将2次top信息写入到文件命令h|? 帮助M 按内存的使用排序P 按cpu使用排序N 以pid的大小排序R 对排序进行反转f 自定义显示字段1 显示所有cpu的负载< 向前> 向后z 彩色W 保存top环境设置
用sar命令监控系统状态
yum install -y sysstat[root@apenglinux-002 ~]# sar -n DEV //查看网卡流量历史 Linux 3.10.0-693.el7.x86_64 (apenglinux-002) 2018年02月21日 _x86_64_ (4 CPU)21时50分02秒 IFACE rxpck/s txpck/s rxkB/s txkB/s rxcmp/s txcmp/s rxmcst/s22时00分01秒 lo 0.00 0.00 0.00 0.00 0.00 0.00 0.0022时00分01秒 ens33 1.29 0.55 0.09 0.08 0.00 0.00 0.00平均时间: lo 0.00 0.00 0.00 0.00 0.00 0.00 0.00平均时间: ens33 1.29 0.55 0.09 0.08 0.00 0.00 0.00
rxpck/s 表示每秒进入收取的包的数量 //这个值大于4000就有问题了,很有可能被***了
txpck/s 表示每秒发送出去的包的数量
rxkB/s 表示每秒收取的数据量,单位为KB //大于5000000就有问题了,很有可能被***了
txkB/s 表示每秒发送的数据量,单位为KB
[root@apenglinux-002 ~]# sar -n DEV 1 5 //时时查看网卡流量 [root@apenglinux-002 ~]# sar -n DEV -f /var/log/sa/sa21 //查看21号的网卡流量历史[root@apenglinux-002 ~]# sar -q //查看历史负载
用nload命令杳看网卡流量
yum install epel-release -y;yum install nload -ynload
按右箭头可以查看其他网卡的网络流量
用free命令查看内存使用情况
[root@apenglinux-002 yum.repos.d]# free -h total used free shared buff/cache availableMem: 1.8G 123M 1.4G 8.6M 298M 1.5GSwap: 2.0G 0B 2.0G
total:内存总大小.total=used+free+buff/cache
used:真正使用的实际内存大小
free:剩余物理内存大小(没有被分配,纯剩余)
shard:共享内存大小
buff/cache:分配给buffer和cache的内存总共有多少。buffer:写入磁盘的缓冲大小;cache:从磁盘中读出的缓存大小
available:系统可使用的内存大小,包含了free。free(buff/cache)+free=available
用ps命令查看系统进程
[root@apenglinux-002 yum.repos.d]# ps auxUSER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMANDroot 1 0.0 0.3 128164 6812 ? Ss 08:19 0:01 /usr/lib/systemd/systemd --switched-root --systemroot 2 0.0 0.0 0 0 ? S 08:19 0:00 [kthreadd]root 3 0.0 0.0 0 0 ? S 08:19 0:00 [ksoftirqd/0]root 5 0.0 0.0 0 0 ? S< 08:19 0:00 [kworker/0:0H]root 7 0.0 0.0 0 0 ? S 08:19 0:00 [migration/0]root 8 0.0 0.0 0 0 ? S 08:19 0:00 [rcu_bh]root 9 0.0 0.0 0 0 ? S 08:19 0:00 [rcu_sched]root 10 0.0 0.0 0 0 ? S 08:19 0:00 [watchdog/0]root 11 0.0 0.0 0 0 ? S 08:19 0:00 [watchdog/1]root 12 0.0 0.0 0 0 ? S 08:19 0:00 [migration/1]root 13 0.0 0.0 0 0 ? S 08:19 0:00 [ksoftirqd/1]root 15 0.0 0.0 0 0 ? S< 08:19 0:00 [kworker/1:0H]root 16 0.0 0.0 0 0 ? S 08:19 0:00 [watchdog/2]root 17 0.0 0.0 0 0 ? S 08:19 0:00 [migration/2]root 18 0.0 0.0 0 0 ? S 08:19 0:00 [ksoftirqd/2]root 19 0.0 0.0 0 0 ? S 08:19 0:00 [kworker/2:0]root 20 0.0 0.0 0 0 ? S< 08:19 0:00 [kworker/2:0H]root 21 0.0 0.0 0 0 ? S 08:19 0:00 [watchdog/3]root 22 0.0 0.0 0 0 ? S 08:19 0:00 [migration/3]root 23 0.0 0.0 0 0 ? S 08:19 0:00 [ksoftirqd/3]root 25 0.0 0.0 0 0 ? S< 08:19 0:00 [kworker/3:0H]root 27 0.0 0.0 0 0 ? S 08:19 0:00 [kdevtmpfs]root 28 0.0 0.0 0 0 ? S< 08:19 0:00 [netns]root 29 0.0 0.0 0 0 ? S 08:19 0:00 [khungtaskd]root 30 0.0 0.0 0 0 ? S< 08:19 0:00 [writeback]root 31 0.0 0.0 0 0 ? S< 08:19 0:00 [kintegrityd]root 32 0.0 0.0 0 0 ? S< 08:19 0:00 [bioset]root 33 0.0 0.0 0 0 ? S< 08:19 0:00 [kblockd]root 34 0.0 0.0 0 0 ? S< 08:19 0:00 [md]root 35 0.0 0.0 0 0 ? S 08:19 0:00 [kworker/1:1]root 40 0.0 0.0 0 0 ? S 08:19 0:00 [kswapd0]root 41 0.0 0.0 0 0 ? SN 08:19 0:00 [ksmd]root 42 0.0 0.0 0 0 ? SN 08:19 0:00 [khugepaged]root 43 0.0 0.0 0 0 ? S< 08:19 0:00 [crypto]root 51 0.0 0.0 0 0 ? S< 08:19 0:00 [kthrotld]root 52 0.0 0.0 0 0 ? S 08:19 0:00 [kworker/u256:1]root 53 0.0 0.0 0 0 ? S< 08:19 0:00 [kmpath_rdacd]root 54 0.0 0.0 0 0 ? S 08:19 0:00 [kworker/3:1]root 56 0.0 0.0 0 0 ? S< 08:19 0:00 [kpsmoused]root 58 0.0 0.0 0 0 ? S< 08:19 0:00 [ipv6_addrconf]root 78 0.0 0.0 0 0 ? S< 08:19 0:00 [deferwq]root 112 0.0 0.0 0 0 ? S 08:19 0:00 [kauditd]root 123 0.0 0.0 0 0 ? R 08:19 0:00 [kworker/2:2]root 243 0.0 0.0 0 0 ? S 08:19 0:00 [kworker/3:2]root 257 0.0 0.0 0 0 ? S< 08:19 0:00 [ata_sff]root 258 0.0 0.0 0 0 ? S< 08:19 0:00 [mpt_poll_0]root 259 0.0 0.0 0 0 ? S< 08:19 0:00 [mpt/0]root 267 0.0 0.0 0 0 ? S 08:19 0:00 [scsi_eh_0]root 268 0.0 0.0 0 0 ? S< 08:19 0:00 [scsi_tmf_0]root 269 0.0 0.0 0 0 ? S 08:19 0:00 [kworker/u256:2]root 270 0.0 0.0 0 0 ? S 08:19 0:00 [scsi_eh_1]root 271 0.0 0.0 0 0 ? S< 08:19 0:00 [scsi_tmf_1]root 272 0.0 0.0 0 0 ? S 08:19 0:00 [scsi_eh_2]root 273 0.0 0.0 0 0 ? S< 08:19 0:00 [scsi_tmf_2]root 276 0.0 0.0 0 0 ? S< 08:19 0:00 [ttm_swap]root 303 0.0 0.0 0 0 ? S< 08:19 0:00 [bioset]root 304 0.0 0.0 0 0 ? S< 08:19 0:00 [xfsalloc]root 305 0.0 0.0 0 0 ? S< 08:19 0:00 [xfs_mru_cache]root 306 0.0 0.0 0 0 ? S< 08:19 0:00 [xfs-buf/sda3]root 307 0.0 0.0 0 0 ? S< 08:19 0:00 [xfs-data/sda3]root 308 0.0 0.0 0 0 ? S< 08:19 0:00 [xfs-conv/sda3]root 309 0.0 0.0 0 0 ? S< 08:19 0:00 [xfs-cil/sda3]root 310 0.0 0.0 0 0 ? S< 08:19 0:00 [xfs-reclaim/sda]root 311 0.0 0.0 0 0 ? S< 08:19 0:00 [xfs-log/sda3]root 312 0.0 0.0 0 0 ? S< 08:19 0:00 [xfs-eofblocks/s]root 313 0.0 0.0 0 0 ? S 08:19 0:00 [xfsaild/sda3]root 381 0.0 0.1 36828 2824 ? Ss 08:19 0:00 /usr/lib/systemd/systemd-journaldroot 405 0.0 0.3 48092 6136 ? Ss 08:19 0:00 /usr/lib/systemd/systemd-udevdroot 442 0.0 0.0 0 0 ? S< 08:19 0:00 [nfit]root 465 0.0 0.0 0 0 ? S< 08:19 0:00 [kworker/0:1H]root 511 0.0 0.0 0 0 ? S< 08:19 0:00 [xfs-buf/sda1]root 512 0.0 0.0 0 0 ? S< 08:19 0:00 [xfs-data/sda1]root 513 0.0 0.0 0 0 ? S< 08:19 0:00 [xfs-conv/sda1]root 514 0.0 0.0 0 0 ? S< 08:19 0:00 [xfs-cil/sda1]root 515 0.0 0.0 0 0 ? S< 08:19 0:00 [xfs-reclaim/sda]root 516 0.0 0.0 0 0 ? S< 08:19 0:00 [xfs-log/sda1]root 517 0.0 0.0 0 0 ? S< 08:19 0:00 [xfs-eofblocks/s]root 522 0.0 0.0 0 0 ? S 08:19 0:00 [xfsaild/sda1]root 545 0.0 0.0 55452 892 ? S
USER 运行进程的用户PID 表示进程的ID%CPU cpu占用率%MEM 内存占用率VSZ 占用虚拟内存RSS 占用实际内存 驻留内存TTY 进程运行的终端STAT 进程的状态 D 不能中断的进程(通常为I/O) R 正在运行中的进程,其中包括了等待cpu时间片的进程 S 已经中断的进程。通常情况下,系统的大部分进程都是这个状态 T 已经停止或者暂停的进程。如果正在运行一个命令,我们按一下ctrl+z暂停进程时,用px命令查看就会显示T这个状态 W 没有足够的内存页分配 X 已经死掉的进程 Z 僵尸进程,即杀不掉、打不死的垃圾进程,占用系统一点资源,不过没有关系。如果占用太多,就需要重视了 < 高优先级进程 N 低优先级进程 L 在内存中被锁了内存分页 s 主进程 l 多线程进程 + 在前台的进程,比如在当前终端执行ps aux就是前台进程START 进程的启动时间TIME 进程占用cpu的总时间COMMAND 进程文件,进程名
ps用到的相关命令
[root@apenglinux-001 ~]# ps aux|grep httpd[root@apenglinux-001 ~]# ps -elf|grep httpd[root@apenglinux-001 ~]# ps aux|grep -c sshd3上例中的3不准确,需要减掉1.因为使用grep命令时,grep命令本身也算是一个进程。[root@localhost ~]# ps aux --sort -rss|less //按rss降序[root@localhost ~]# ps aux --sort rss |less //按rss升序[root@localhost ~]# ps auxf|grep sshd[root@localhost ~]# ps axo pid,tty,command //自定义字段 PID TT COMMAND 1 ? /usr/lib/systemd/systemd --switched-root --system --deserialize 21 2 ? [kthreadd] 3 ? [ksoftirqd/0] 以不省略