-1

I have created a script to stress the cpu, vm and io of a virtual machine and write the results to a file. I have written the script in such a way that it will read user input for how long in seconds they would like to run each test. However, I have had to hard code the number of cpus etc as 4. How can I change this script so that the user can select how many cpu/vm/io to stress in addition to how long?

The script is as follows:

#!/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
paul
  • 197
  • 1
  • 2
  • 12

2 Answers2

1
grep -c ^processor /proc/cpuinfo

Will give you the number cores that the machine has

Alex
  • 7,538
  • 23
  • 84
  • 152
  • can this allow the user to select how many to run though? for example i'd like the system to say "how many cpus would you like to stress" and for it to read the user input if they type 2. will this do the same thing? – paul Feb 27 '15 at 10:15
  • Just save that value in a variable, then do a read for user's choice. If he enters one, use that one, otherwise... use the previously saved value – Alex Feb 27 '15 at 14:25
  • ok it is reading them now. I want to compare these to how vmstat etc run without stress. Can stress be written to text files/csv for me to graph? can it be run at the same time as vmstat etc? would you now how to do this? – paul Feb 27 '15 at 23:27
1

You just need to read the number of cores into a variable:

echo -n "Number of cores: "
read cores

Add this at the position you find most suitable. To use the variable instead of the hard coded values, replace each occurence of 4 with "$cores".

A small hint in case you choose to turn your back on Java and become a Bash enthusiast: In order to avoid typical problems with spaces and special characters, it is a best practice to always enclose bash variables in quotes ("$cores") or curly braces (${cores}).

Michael Jaros
  • 4,586
  • 1
  • 22
  • 39
  • ok it is reading them now. I want to compare these to how vmstat etc run without stress. Can stress be written to text files/csv for me to graph? can it be run at the same time as vmstat etc? would you now how to do this? – paul Feb 27 '15 at 21:42