#!/bin/bash ############################################################## # File Name: /sources/used.sh # Version: V1.0 # Author: sanshi # Organization: https://www.9133w.cn/sources/ # Created Time : 2021-06-15 16:25:36 # Description: ############################################################## while true do case $1 in -m) mem_set=$2; shift 2 ;; -c) cpu_set=$2; shift 2 ;; -d) action=$2; shift 2 ;; *) break esac done mem_start() { stress_cmd=`command -v stress-ng` if [ -z "$stress_cmd" ] then wget https://9133w.cn/sources/stress-ng.tgz && \ tar xf stress-ng.tgz && \ pushd stress-ng && \ make && \ make install && popd [ $? -ne 0 ] && yum install -y stress-ng fi stress_cmd=`command -v stress-ng` [ -z "$stress_cmd" ] && echo 'stress-ng command not find.' && exit 1 nohup $stress_cmd -vm 2 --vm-bytes ${mem_set}% -t 1y & } cpu_start() { rpm -q cpulimit &> /dev/null || yum install -y cpulimit if ! command -v cpulimit; then echo 'cpulimit not installed in your system.' exit 1 fi cpulimit_cmd=`command -v cpulimit` cpulimit_pid=`ps -ef | grep 'cpulimit' | grep -v grep | awk '{print $2}'` dd_pid=`ps -ef|grep 'dd if' |grep -v grep | awk '{print $2}'` if [ -z "$dd_pid" ]; then nohup dd if=/dev/zero of=/dev/null & fi sleep 1 dd_pid=`ps -ef|grep 'dd if' |grep -v grep | awk '{print $2}'` if [ -z "$cpulimit_pid" ]; then nohup $cpulimit_cmd -p $dd_pid -l $cpu_set & fi } ac_stop() { dd_pid=`ps -ef|grep 'dd if' |grep -v grep | awk '{print $2}'` [ -n "$dd_pid" ] && kill -9 $dd_pid pgrep stress-ng && pkill stress-ng } if [ -n "$mem_set" -a -n "$cpu_set" ] then cpu_start mem_start elif [ -n "$mem_set" ] then mem_start elif [ -n "$cpu_set" ] then cpu_start elif [ -n "$action" ] then ac_stop else echo "usage: $0 -m 60(使用率) -c 80(cpu使用率) ['-d stop' to stop all stresses]" fi