#!/bin/bash #获取网卡名称 network_adapter=`ip a|grep "BROADCAST"|awk '{print $2}'|awk -F: '{print $1}'` function Net_speed_limiting { #输入提示 read -p "Please input Net speed limiting rate(kbit): " Net_rate read -p "Waiting time(s): " wait_time #echo -e "Please input Net speed limiting rate: \c\t " #与单位拼接成字符串 Net_rate_=$Net_rate"kbit" #带宽限制命令 tc qdisc add dev $network_adapter root tbf rate $Net_rate latency 50ms burst 1000 >/dev/null 2>&1 & #等待时间,单位s for i in $(seq 1 $wait_time) do echo -ne "Count down $wait_time \r" #不换行刷新数据 sleep 1 ((wait_time--)); done #解除带宽限制命令 tc qdisc del dev $network_adapter root } function CPU_limiting { #输入提示 read -p "Please input CPU limiting numbers: " CPU_numbers #使n个CPU利用率达到100% stress -c $CPU_numbers >/dev/null 2>&1 & } function MEM_limiting { #输入提示 read -p "Please input process numbers: " process_numbers read -p "Please input limiting MEM numbers(M) of per process: " MEM_numbers MEM_numbers_=$MEM_numbers"M" #起几个进程,每个进程占n M的内存 stress --vm $process_numbers --vm-bytes $MEM_numbers_ --vm-keep >/dev/null 2>&1 & } function status { echo "======限流状态======" tc -s qdisc ls dev $network_adapter echo "======限CPU状态======" ps -ef|grep stress |grep -v grep |grep c if [ $? -ne 0 ]; then echo "CPU没限制" fi echo "======限内存状态======" ps -ef|grep stress |grep -v grep |grep vm if [ $? -ne 0 ]; then echo "内存没限制" fi echo "======tips======" echo "pkill -9 stress 或 kill -9 PID 可解除CPU和内存限制" echo "限流倒计时结束,限制也解除" } case "$1" in "Net_speed_limiting") Net_speed_limiting ;; "CPU_limiting") CPU_limiting ;; "MEM_limiting") MEM_limiting ;; "status") status ;; "" |*) echo "Usage: $0 [ Net_speed_limiting | CPU_limiting | MEM_limiting | status ]"; echo "" ;; esac exit 0
命令详情参考其他博主
标签:stress,grep,echo,numbers,模拟系统,Net,limiting,高时,CPU From: https://www.cnblogs.com/daizichuan/p/17487541.html