How to Simulate CPU, Memory, and Disk Load on Linux with Stress and dd
This guide explains how to use the stress and dd utilities together with a custom Bash script to artificially consume CPU, memory, and disk resources on idle cloud servers, helping you demonstrate load without affecting production workloads.
Background
In many cloud environments, idle virtual machines may be flagged for downsizing, which can reduce budget allocations. To avoid this, administrators sometimes need to generate artificial load on the servers to appear busy.
Approach
The solution combines the stress tool for CPU, memory, and I/O pressure with the dd command to fill disk space. A Bash script orchestrates these actions, monitors resource usage, and stops the load after a configurable period (default 20 minutes).
Stress Tool Overview
Stress can launch multiple worker processes that consume specific resources: stress -c 4 – creates four CPU‑bound processes. stress --vm 2 --vm-bytes 300M --vm-keep – allocates 300 MiB in two processes and keeps it in memory. stress -i 4 – runs four I/O workers that repeatedly call sync(). stress -d 1 --hdd-bytes 10M – creates and deletes 10 MiB files continuously.
Key options include --vm-keep, --vm-hang N, --vm-stride B, and timeout controls such as --timeout N. The article provides several command examples illustrating how each option affects CPU and memory usage.
dd Command Overview
dd copies data between files or devices and can be used to generate large files quickly: dd if=/dev/zero of=sun.txt bs=1M count=1 Important parameters are if (input file), of (output file), bs (block size), and count (number of blocks). The guide also lists common use‑cases such as creating swap files, wiping disks, and testing I/O performance.
Complete Bash Script
The script performs the following steps:
Detects whether stress is installed; installs it if missing.
Calculates current memory utilization and, if below 80 %, launches a memory‑stress process using stress --vm … --vm-keep.
Checks for a second data disk (e.g., /dev/vdb), creates a temporary directory, and uses dd to fill 80 % of the free space with a large file.
Monitors memory usage in a loop; when utilization reaches 80 % it kills the stress processes, logs the event, and restarts the memory stress after a short pause.
Stops the entire test after 1200 seconds (20 minutes) and cleans up any remaining stress processes.
All actions are logged to script_log.log with timestamps.
Execution Results
Sample screenshots show the impact on CPU, memory, and I/O while the script runs:
Limitations and Considerations
The script deliberately avoids heavy CPU stress to prevent service disruption or unexpected reboots. Disk‑space consumption is limited to 80 % of the target volume, and the script cleans up the temporary file once the threshold is reached. Adjust the thresholds, durations, and number of workers to match your environment.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
