linux的CPU占用脚本、内存占用脚本和硬盘IO测试代码,2024年最新看完直呼内行

pid_array[ i ] = i]= i]=!;

done

for i in “${pid_array[@]}”; do

echo ‘execute: kill’ $i ;

echo ‘kill’ $i >> /root/file.txt

done

echo “If executed ctrl+C,Please execute the above lines manually”

echo “Please wait $2 seconds”

sleep $2

for i in awk '{print $2}' /root/file.txt ; do

echo “kill $i”

kill $i

done

[root@centos-73-iso-100g-test ~]#

测试


  • 执行前,重新打开一个窗口,执行top并按一下1,会列出所有cpu的使用率

top - 21:43:41 up 13 min, 3 users, load average: 0.00, 0.01, 0.04

Tasks: 194 total, 1 running, 193 sleeping, 0 stopped, 0 zombie

%Cpu0 : 0.0 us, 0.3 sy, 0.0 ni, 99.7 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st

%Cpu1 : 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st

%Cpu2 : 0.0 us, 0.3 sy, 0.0 ni, 99.7 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st

%Cpu3 : 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st

%Cpu4 : 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st

%Cpu5 : 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st

%Cpu6 : 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st

%Cpu7 : 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st

KiB Mem : 16267428 total, 15500496 free, 392744 used, 374188 buff/cache

KiB Swap: 10485756 total, 10485756 free, 0 used. 15559560 avail Mem

  • 执行:sh cpu.sh 3 120 【意思是占用3颗cpu,占用120秒】

如果参数不够,该脚本是会报错的

[root@centos-73-iso-100g-test ~]# sh cpu.sh 3

USAGE: cpu.sh <cpus,sleep time>

[root@centos-73-iso-100g-test ~]#

执行以后可以就会出现下面内容【此时该脚本并不会结束,而是处于 sleep 120状态

[root@centos-73-iso-100g-test ~]# sh cpu.sh 3 120

execute: kill 3083

execute: kill 3085

execute: kill 3087

If executed ctrl+C,Please execute the above lines manually

Please wait 120 seconds

  • 回到刚才执行top的窗口中,可以看到有3颗cpu的使用率为100%

[root@centos-73-iso-100g-test ~]# top

top - 21:45:21 up 15 min, 3 users, load average: 0.67, 0.16, 0.09

Tasks: 199 total, 4 running, 195 sleeping, 0 stopped, 0 zombie

%Cpu0 : 0.0 us, 0.0 sy, 0.0 ni, 99.7 id, 0.3 wa, 0.0 hi, 0.0 si, 0.0 st

%Cpu1 :100.0 us, 0.0 sy, 0.0 ni, 0.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st

%Cpu2 :100.0 us, 0.0 sy, 0.0 ni, 0.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st

%Cpu3 : 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st

%Cpu4 : 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st

%Cpu5 : 0.0 us, 0.3 sy, 0.0 ni, 99.7 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st

%Cpu6 : 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st

%Cpu7 :100.0 us, 0.0 sy, 0.0 ni, 0.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st

KiB Mem : 16267428 total, 15499112 free, 394012 used, 374304 buff/cache

KiB Swap: 10485756 total, 10485756 free, 0 used. 15558272 avail Mem

  • 120秒以后,该脚本就结束了,并自动kill掉这几个进程

[root@centos-73-iso-100g-test ~]# sh cpu.sh 3 120

execute: kill 3083

execute: kill 3085

execute: kill 3087

If executed ctrl+C,Please execute the above lines manually

Please wait 120 seconds

kill 3083

kill 3085

kill 3087

[root@centos-73-iso-100g-test ~]#

  • 此时 top界面的cpu使用率也被释放了

[root@centos-73-iso-100g-test ~]# top

top - 21:47:48 up 17 min, 3 users, load average: 1.26, 0.84, 0.37

Tasks: 193 total, 1 running, 192 sleeping, 0 stopped, 0 zombie

%Cpu0 : 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st

%Cpu1 : 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st

%Cpu2 : 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st

%Cpu3 : 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st

%Cpu4 : 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st

%Cpu5 : 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st

%Cpu6 : 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st

%Cpu7 : 0.8 us, 0.0 sy, 0.0 ni, 99.2 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st

KiB Mem : 16267428 total, 15499472 free, 393652 used, 374304 buff/cache

KiB Swap: 10485756 total, 10485756 free, 0 used. 15558620 avail Mem

内存占用

===================================================================

代码形式


代码

  • 这个脚本功能如下【这个脚本不难,代码就不做解释了】

  • 1、想占用多少内存【单位是M(不能超过现有内存,否则可能会蹦)】

  • 2、占用多长时间【单位是秒】

  • 3、占用时间到达以后,会自动释放被占用的内存【如果提前结束,需要手动删除占用文件】

[root@centos-76-qcow2-50g-3 ~]# free -g

total used free shared buff/cache available

Mem: 62 0 62 0 0 61

Swap: 0 0 0

[root@centos-76-qcow2-50g-3 ~]#

[root@centos-76-qcow2-50g-3 ~]# cat free.sh

#!/bin/bash

if [ $# != 2 ]; then

echo “USAGE: $0 <free,sleep time>”

exit 1;

fi

free -m > /tmp/freee

cat /tmp/freee

mkdir /tmp/memory

mount -t tmpfs -o size=$1M tmpfs /tmp/memory

dd if=/dev/zero of=/tmp/memory/block

free -m > /tmp/freee

cat /tmp/freee

echo “If executed ctrl+C,Please execute the following lines manually”

echo “execute: rm -rf /tmp/memory/block”

echo “execute: umount /tmp/memory”

echo “execute: rmdir /tmp/memory”

echo “Please wait $2 seconds”

sleep $2

rm -rf /tmp/memory/block

umount /tmp/memory

rmdir /tmp/memory

[root@centos-76-qcow2-50g-3 ~]#

测试

  • 执行:sh free.sh 2048 120【2048是内存数量,单位是M,120是时间,单位是秒】

  • 因为我这台虚机有64,所以我定义的是20480,占用20G

如果参数不够 会报错的

[root@centos-76-qcow2-50g-3 ~]# sh free.sh 1024

USAGE: free.sh <free,sleep time>

[root@centos-76-qcow2-50g-3 ~]#

执行以后可以就会出现下面内容【此时该脚本并不会结束,而是处于 sleep 120状态

[root@centos-76-qcow2-50g-3 ~]# sh free.sh 20480 120

total used free shared buff/cache available

Mem: 64264 451 63559 16 253 63300

Swap: 0 0 0

dd: writing to ‘/tmp/memory/block’: No space left on device

41943041+0 records in

41943040+0 records out

21474836480 bytes (21 GB) copied, 67.0879 s, 320 MB/s

total used free shared buff/cache available

Mem: 64264 451 43033 20496 20779 42797

Swap: 0 0 0

If executed ctrl+C,Please execute the following lines manually

execute: rm -rf /tmp/memory/block

execute: umount /tmp/memory

execute: rmdir /tmp/memory

Please wait 120 seconds

  • 这个占用的是 shared共享内存,可以看到数量已经有变化了,并且free可用内存也相应减少了

在这里插入图片描述

  • 等待120秒以后呢,该脚本运行结束,并且占用的内存也会被释放

[root@centos-76-qcow2-50g-3 ~]# sh free.sh 20480 120

total used free shared buff/cache available

Mem: 64264 451 63559 16 253 63300

Swap: 0 0 0

dd: writing to ‘/tmp/memory/block’: No space left on device

41943041+0 records in

41943040+0 records out

21474836480 bytes (21 GB) copied, 67.0879 s, 320 MB/s

total used free shared buff/cache available

Mem: 64264 451 43033 20496 20779 42797

Swap: 0 0 0

If executed ctrl+C,Please execute the following lines manually

execute: rm -rf /tmp/memory/block

execute: umount /tmp/memory

execute: rmdir /tmp/memory

Please wait 120 seconds

[root@centos-76-qcow2-50g-3 ~]#

[root@centos-76-qcow2-50g-3 ~]# free -m

total used free shared buff/cache available

Mem: 64264 451 63559 16 253 63300

Swap: 0 0 0

[root@centos-76-qcow2-50g-3 ~]#

rpm包的方式


rpm包下载安装

下载好以后上传到linux主机上,然后通过下面方法安装好。

[root@ccx ~]# ls /opt

memload-7.0-1.r29766.x86_64.rpm

[root@ccx ~]# rpm -ivh /opt/memload-7.0-1.r29766.x86_64.rpm

Preparing… ################################# [100%]

Updating / installing…

1:memload-7.0-1.r29766 ################################# [100%]

[root@ccx ~]#

占用测试

  • 基本环境弄好了,我们现在开始消耗内存看看【占用内存不要超过总内存】

我下面是在多个窗口中,注意看主机名 的变化

可以看到node2现在消耗了1G内存

[root@ccx ~]# free -g

total used free shared buff/cache available

Mem: 31 1 28 0 1 29

Swap: 0 0 0

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Linux运维工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Linux运维全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
img
img
img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Linux运维知识点,真正体系化!

由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新

如果你觉得这些内容对你有帮助,可以添加VX:vip1024b (备注Linux运维获取)
img

最后的话

最近很多小伙伴找我要Linux学习资料,于是我翻箱倒柜,整理了一些优质资源,涵盖视频、电子书、PPT等共享给大家!

资料预览

给大家整理的视频资料:

给大家整理的电子书资料:

如果本文对你有帮助,欢迎点赞、收藏、转发给朋友,让我有持续创作的动力!

一个人可以走的很快,但一群人才能走的更远。不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎扫码加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
img

都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新**

如果你觉得这些内容对你有帮助,可以添加VX:vip1024b (备注Linux运维获取)
[外链图片转存中…(img-zAfk1UN3-1712683294007)]

最后的话

最近很多小伙伴找我要Linux学习资料,于是我翻箱倒柜,整理了一些优质资源,涵盖视频、电子书、PPT等共享给大家!

资料预览

给大家整理的视频资料:

[外链图片转存中…(img-VKl634nQ-1712683294008)]

给大家整理的电子书资料:

[外链图片转存中…(img-iuGTRYQg-1712683294009)]

如果本文对你有帮助,欢迎点赞、收藏、转发给朋友,让我有持续创作的动力!

一个人可以走的很快,但一群人才能走的更远。不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎扫码加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
[外链图片转存中…(img-J46XQ8dX-1712683294010)]

  • 7
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值