Bash Script CPU Stress Test Example

The Bash Script CPU Stress Test Example is a powerful tool designed to evaluate CPU performance under load using the stress utility. This script ensures that the required tool is installed before proceeding to execute a stress test based on user-defined parameters. By accepting the number of CPU cores to stress and the duration of the test, it allows for a customized benchmarking experience. This article will guide you through the prerequisites, the script explanation, and the steps to run it effectively.

Prerequisites

  • A basic understanding of Bash scripting
  • Familiarity with command line operations
  • Access to a Linux-based system
  • Installation of the stress package

DID YOU KNOW?

The stress utility can simulate various workloads, not just CPU usage. It can also stress test I/O, memory, and other system components.

The Script

This Bash script begins by checking whether the stress tool is installed. If it isn’t, the script installs it. It then accepts two command-line arguments: the number of CPU cores to stress and the duration of the stress test, validating these inputs to ensure that they are positive integers. If all checks are passed, the script executes the stress command using the specified parameters and provides feedback to the user throughout the process.

#!/bin/bash

# Check if stress tool is installed
if ! command -v stress &> /dev/null
then
    echo "stress tool not found. Installing..."
    sudo apt-get install -y stress
fi

# Check for input arguments
if [ "$#" -ne 2 ]; then
    echo "Usage: $0  "
    exit 1
fi

cores=$1
duration=$2

# Validate input
if ! [[ "$cores" =~ ^[0-9]+$ ]] || ! [[ "$duration" =~ ^[0-9]+$ ]]; then
    echo "Both arguments must be positive integers."
    exit 1
fi

echo "Starting CPU stress test on $cores cores for $duration seconds..."

# Run stress test
stress --cpu "$cores" --timeout "$duration"

echo "Stress test completed!"

Step-by-Step Explanation

NOTE!

Ensure you have sudo privileges for installing packages.

In this section, we will break down the script into manageable steps to understand how each part works.

  1. Check Installation: The script first checks whether the stress tool is installed on your system. If not, it installs it using apt-get.
  2. Input Validation: It ensures that the user provides exactly two input arguments: the number of CPU cores and the duration of the test. It also validates these inputs to guarantee they are positive integers.
  3. Run the Stress Test: If the inputs are valid, it invokes the stress command, stressing the specified number of CPU cores for the designated duration.
  4. Provide Feedback: Throughout the process, the script informs the user about the installation, commencement of the test, and completion status.
Bash Script CPU Stress Test Example
Bash Script CPU Stress Test Example

How to Run the Script

To execute this script, follow these simple steps:

  1. Create a new file, for example, script.sh, and paste the script into it.
  2. Make the script executable by running chmod +x script.sh.
  3. Run the script with the number of CPU cores and duration as arguments: ./script.sh .

Conclusion

Running a CPU stress test can be crucial for evaluating system performance and stability. This Bash script provides a straightforward and effective method for users to conduct stress tests tailored to their system’s capabilities. By following the steps outlined, you can implement and customize your tests seamlessly.

FAQ

  1. What should I do if the stress tool fails to install?

    Ensure your system’s package manager is configured correctly and that you have internet access. You can also try updating the package list with sudo apt-get update.

  2. Can I specify more cores than I have?

    If you specify more cores than available, stress will utilize all available cores, and additional requests will be ignored.

  3. Is it safe to run this script?

    Yes, as long as you monitor system temperatures and load, it’s safe to test CPU performance under load.

  4. Can I customize the stress test further?

    Yes, the stress command offers various options to stress memory, I/O, and other resources. Refer to the man stress page for more details.

  5. How do I determine my CPU core count?

    You can check your CPU core count by running nproc or looking at the output of lscpu.