linux的CPU占用脚本、内存占用脚本和硬盘IO测试代码

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

代码


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

  • 1、想占用多少颗CPU【被占用的cpu使用率会是100%】

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

  • 3、占用时间到达以后,会自动kill掉相关进程【如果提前结束,需要手动结束占用进程】

[root@centos-73-iso-100g-test ~]# lscpu | grep CPU(s):

CPU(s): 8

NUMA node0 CPU(s): 0-7

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

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

#!/bin/bash

rm -rf /root/file.txt

endless_loop()

{

echo -ne "i=0;

while true ;do

i=i+100;

i=100;done" | /usr/bin/bash &

}

if [ $# != 2 ]; then

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

exit 1;

fi

for i in seq $1

do

endless_loop

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<

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值