bash-control要压缩多少cpu / vm / io

时间:2015-02-27 09:34:07

标签: linux bash ubuntu-12.04 stress-testing debian-7.6.0

我创建了一个脚本来强调虚拟机的cpu,vm和io,并将结果写入文件。我已经编写了这样一个脚本,它将读取用户输入他们想要运行每个测试的时间长度。但是,我不得不硬编码cpus等的数量为4.如何更改此脚本以便用户可以选择多少cpu / vm / io除了多长时间之外还要加压?

脚本如下:

#!/bin/bash
#
# Script to get stress statistics#

echo "Enter the number of intervals (seconds) for running the stress statistics:"


read seconds_to_run
# the variable "seconds_to_run" now has the value entered by keyboard

# The stress option chosen will be output to the corresponding file and timestamped with today's date.
out_cpufilename="/tmp/stress_cpu_stat_"$( date +"%B.%d" )
out_vmfilename="/tmp/stress_vm_stat_"$( date +"%B.%d" )
out_iofilename="/tmp/stress_io_stat_"$( date +"%B.%d" )


while true; do
    clear
    echo "*******************************"
    echo "* Choose from the following: *"
    echo "*******************************"
    echo "* [1] To stress cpu statistics *"
    echo "* [2] To view stress vm statistics *"
    echo "* [3] To view stress io statistics *"
    echo "Press A to quit."
    echo "************************"
    echo -n "Enter your menu choice [1-3]: "

    read choice


    case  $choice in    
        1) echo "stress cpu statistics"
            stress -c 4 -t $choice |tee $out_cpufilename 
            echo "This file will be saved to $out_cpufilename"
            sleep 3 ;;

        2) echo "stress cpu statistics"
            stress -m 4 -t $choice |tee $out_vmfilename 
            echo "This file will be saved to $out_vmfilename"
            sleep 3 ;;

        3) echo "stress cpu statistics"
            stress -i 4 -t $choice |tee $out_iofilename 
            echo "This file will be saved to $out_iofilename"
            sleep 3 ;;


        A) #Quit option
            echo You have chosen to quit.
            exit;;   

        *) #Wildcard option in case user enters an invalid option such as "e"
            echo Invalid choice, please make another choice
            sleep 3;;


    esac
done

2 个答案:

答案 0 :(得分:1)

grep -c ^processor /proc/cpuinfo

将为您提供机器具有的核心数

答案 1 :(得分:1)

您只需要将核心数量读入变量:

echo -n "Number of cores: "
read cores

在您认为最合适的位置添加此项。要使用变量而不是硬编码值,请将每个出现4替换为"$cores"

如果您选择退回Java并成为Bash爱好者,这是一个小提示:为了避免空格和特殊字符的典型问题,最好将bash变量括在引号中({{1 }}或花括号("$cores")。