Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # Version 14
- # POSTED ONLINE: https://pastebin.com/4wZk0FxU
- # WARNING: This script will run a CPU stress test on the processor resulting in high temperatures and power draw.
- # Prolonged usage may cause system instability, degradation, or damage.
- # Do not leave the system unattended during use.
- # Do not run this script unless adequate cooling is in place and you understand the risks.
- # You assume all responsibility for using this script.
- # Installation:
- # $ sudo cp /media/D/Users/Wolf/Files/Archives/Scripts/Shell/TestCpu/testcpu /usr/local/bin/
- # $ sudo chmod +x /usr/local/bin/testcpu <-- Needed to fix permission denied error.
- #
- # NOTE: Ensure 'tmux' and 'stress-ng' are installed: $ sudo pacman -S tmux stress-ng
- # Usage:
- # This script stress-tests CPU cores with 'stress-ng' and displays live system logs using 'journalctl' in a split tmux session.
- #
- # 1. Run the script with: $ testcpu [CPU_LIST]
- #
- # NOTE: CPU_LIST is optional and specifies which logical CPUs to test.
- # It supports comma-separated values and ranges, e.g., "0-3,6,8".
- # If omitted, all logical CPUs (0 through nproc-1) are tested sequentially.
- #
- # Examples:
- # $ testcpu # Test all cores sequentially.
- # $ testcpu 0-3,6,8 # Test cores 0 to 3, 6, and 8.
- # $ testcpu --help # Show help.
- #
- # 2. This script will launch a tmux session named 'cpu_test' with two panes:
- # The top pane runs the stress test.
- # The bottom pane displays live journalctl events.
- # NOTE: This script runs continuously until manually stopped.
- #
- # 3. To stop this script:
- # From inside tmux: press Ctrl+C in each pane and run $ exit
- # From outside tmux: $ sudo tmux kill-session -t cpu_test
- # NOTE: If tmux or stress-ng are still running then terminate them using System Monitor or any process manager you prefer.
- show_help() {
- cat << EOF
- Usage: $(basename "$0") [OPTIONS] [CPU_LIST]
- Options:
- -h, --help Show this help message and exit.
- CPU_LIST:
- Optional comma-separated list of logical CPUs or CPU ranges to test.
- Examples:
- 0-3 Test CPUs 0,1,2,3
- 1,3,5 Test CPUs 1,3,5
- 0-2,4,6-7 Test CPUs 0,1,2,4,6,7
- If no CPU_LIST is provided, all CPUs (0 through $(nproc - 1)) are tested.
- Example:
- $0 0-3,5
- EOF
- }
- parse_cpu_list() {
- local input=$1
- local -a list=()
- IFS=',' read -ra parts <<< "$input"
- for part in "${parts[@]}"; do
- if [[ $part =~ ^([0-9]+)-([0-9]+)$ ]]; then
- start=${BASH_REMATCH[1]}
- end=${BASH_REMATCH[2]}
- if (( start > end )); then
- echo "Invalid CPU range: $part"
- exit 1
- fi
- for ((i=start; i<=end; i++)); do
- list+=("$i")
- done
- elif [[ $part =~ ^[0-9]+$ ]]; then
- list+=("$part")
- else
- echo "Invalid CPU list format: $part"
- exit 1
- fi
- done
- # Remove duplicates and sort.
- IFS=$'\n' sorted=($(sort -n <<<"${list[*]}"))
- unset IFS
- # Unique filter.
- cpus=()
- prev=-1
- for c in "${sorted[@]}"; do
- if [[ $c -ne $prev ]]; then
- cpus+=("$c")
- prev=$c
- fi
- done
- }
- # Handle help option.
- if [[ "${1:-}" == "-h" || "${1:-}" == "--help" ]]; then
- show_help
- exit 0
- fi
- # If tmux is not installed.
- if ! command -v tmux >/dev/null 2>&1; then
- echo -e "\e[94m::\e[0m tmux not found. Install with: $ sudo pacman -S tmux"
- exit 1
- fi
- # If stress-ng is not installed.
- if ! command -v stress-ng >/dev/null 2>&1; then
- echo -e "\e[94m::\e[0m stress-ng not found. Install with: $ sudo pacman -S stress-ng"
- exit 1
- fi
- # Check if script is running as root, if not re-run with sudo.
- if [[ $EUID -ne 0 ]]; then
- echo "Requesting administrative privileges . . ."
- exec sudo "$0" "$@"
- fi
- # Get total logical CPUs.
- max_cpu=$(( $(nproc) - 1 ))
- # Determine CPU indexes to test.
- if [[ -n "${1:-}" ]]; then
- parse_cpu_list "$1"
- cpu_warning="logical CPUs: $(IFS=,; echo "${cpus[*]}")"
- else
- cpus=($(seq 0 "$max_cpu"))
- cpu_warning="all logical CPUs"
- fi
- # Validate CPUs are within range.
- for cpu in "${cpus[@]}"; do
- if (( cpu < 0 || cpu > max_cpu )); then
- echo "ERROR: CPU number out of range: $cpu"
- exit 1
- fi
- done
- while true; do
- echo
- echo -e "\e[91m:: WARNING: This script will run a CPU stress test on $cpu_warning resulting in high temperatures and power draw.\e[0m"
- echo -e "\e[91m Prolonged usage may cause system instability, degradation, or damage.\e[0m"
- echo -e "\e[91m Do not leave the system unattended during use.\e[0m"
- echo -e "\e[91m Do not continue unless adequate cooling is in place and you understand the risks.\e[0m"
- echo -e "\e[91m You assume all responsibility for using this script.\e[0m"
- echo
- read -p "Do you want to continue? [Y/N]: " response
- case "$response" in
- y|Y) break ;;
- n|N) exit 0 ;; # Exit script with status 0 to indicate user cancellation.
- *) echo ;;
- esac
- done
- echo
- while true; do
- read -p "Do you want to include a RAM test? [Y/N]: " ram_response
- case "$ram_response" in
- y|Y) include_ram_test=true; break ;;
- n|N) include_ram_test=false; break ;;
- *) echo ;;
- esac
- done
- echo
- # Pass cpus array as string to embedded script.
- cpus_str="$(IFS=,; echo "${cpus[*]}")"
- # Create a temporary file.
- stress_script=$(mktemp)
- cleanup() {
- # Delete the temporary script file.
- [[ -n "$stress_script" ]] && rm -f "$stress_script"
- }
- # Run cleanup() function on script interruption or termination.
- trap cleanup SIGINT SIGTERM SIGHUP SIGQUIT EXIT
- # Write the contents of the temporary script.
- cat > "$stress_script" <<EOF
- #!/bin/bash
- cleanup() {
- [[ -n "\${stress_pid}" && "\${stress_pid}" =~ ^[0-9]+\$ ]] && kill "\${stress_pid}" 2>/dev/null
- exit 1
- }
- # Run cleanup() function on script interruption or termination.
- trap cleanup SIGINT SIGTERM SIGHUP SIGQUIT EXIT
- seconds_per_test=15 # The duration of the cpu tests.
- seconds_between_tests=3 # The delay between cpu tests (gives time to read the output and for the processor to cool).
- max_cycles=2 # The maximum number of full test cycles to perform before exiting.
- cpus_str="\$1"
- include_ram_test="\$2"
- IFS=',' read -ra cpus <<< "\$cpus_str"
- num_cpus=\${#cpus[@]}
- total_time=\$((num_cpus * (seconds_per_test + seconds_between_tests)))
- blue="\e[94m::\e[0m"
- yellow="\e[93m::\e[0m"
- red="\e[91m::\e[0m"
- cycle_count=0
- while true; do
- # Loop through the specified CPUs, stress-testing each sequentially.
- for idx in "\${!cpus[@]}"; do
- cpu="\${cpus[\$idx]}"
- # clear # NOTE: Comment out this line if you want to retain the output from past tests instead of clearing it.
- time_elapsed=\$((idx * (seconds_per_test + seconds_between_tests)))
- time_remaining=\$((total_time - time_elapsed))
- if (( num_cpus > 1 )); then
- progress=\$(( idx * 100 / (num_cpus - 1) ))
- else
- progress=100
- fi
- eta=\$(printf '%02d:%02d:%02d' \$((time_remaining / 3600)) \$(((time_remaining % 3600) / 60)) \$((time_remaining % 60)))
- echo -e "\${yellow} Testing CPU \${cpu} [Progress: \${progress}% ETA: \${eta}]\n"
- if [[ "\$include_ram_test" == "true" ]]; then
- taskset -c "\${cpu}" stress-ng \
- --cpu 1 \
- --cpu-method all \
- --vm 1 \
- --vm-bytes 90% \
- --vm-method all \
- --verify \
- --vm-keep \
- --metrics-brief \
- --timeout "\${seconds_per_test}s" &
- else
- taskset -c "\${cpu}" stress-ng \
- --cpu 1 \
- --cpu-method all \
- --metrics-brief \
- --timeout "\${seconds_per_test}s" &
- fi
- stress_pid=\$!
- wait "\${stress_pid}"
- unset stress_pid
- echo -e "\n\${blue} Cooling down for \${seconds_between_tests} seconds before continuing . . .\n"
- sleep "\${seconds_between_tests}"
- done
- ((cycle_count++))
- echo -e "\${red} Completed \${cycle_count} of \${max_cycles} test cycles . . .\n"
- if (( cycle_count >= max_cycles )); then
- exit 0
- fi
- done
- EOF
- chmod +x "$stress_script"
- session_name="stress_test"
- # If session exists.
- if tmux has-session -t "$session_name" 2>/dev/null; then
- tmux kill-session -t "$session_name"
- fi
- # Start tmux session with CPU test in top pane.
- tmux new-session -d -s "$session_name"
- # Split directly from CPU pane to create bottom pane.
- tmux split-window -v -t "$session_name:0"
- bottom_pane="$session_name:0.1"
- # Run CPU test in top pane.
- tmux send-keys -t "$session_name:0.0" "\"$stress_script\" \"$cpus_str\" \"$include_ram_test\"" C-m
- # Run journalctl in bottom pane.
- tmux send-keys -t "$bottom_pane" "journalctl -fk -o short-iso" C-m
- # Label panes.
- tmux select-pane -t "$session_name:0.0" -T "CPU"
- tmux select-pane -t "$bottom_pane" -T "LOG"
- # Set layout and attach.
- tmux select-layout -t "$session_name" even-vertical
- tmux attach -t "$session_name"
Advertisement
Comments
-
- Great work! I really enjoyed reading your post. I’ve recently published an article on my website that covers a wide range of topics including Trending Flash News, Bangla and English news, current events, politics, entertainment, sports, job updates, international headlines, business, science, technology, and more. Feel free to check it out here: https://www.trendingflashnews.com/
Add Comment
Please, Sign In to add comment