Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # Version 7
- # Installation:
- # $ sudo cp ./testcpu /usr/local/bin/
- # $ sudo chmod +x /usr/local/bin/testcpu
- # This script runs 'stress-ng' on each CPU one at a time,
- # while showing live system logs via journalctl in a split tmux session.
- #
- # Usage:
- # 1. Ensure 'tmux' and 'stress-ng' are installed: $ sudo pacman -S tmux stress-ng
- #
- # 2. Run the script with: $ testcpu
- #
- # 3. 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.
- #
- # 4. 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.
- # If tmux is not installed.
- if ! command -v tmux >/dev/null 2>&1; then
- echo -e "\e[94m::\e[0m tmux could not be 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 could not be 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
- SESSION="cpu_test"
- # Number of logical CPUs
- NUM_CPUS=$(nproc)
- # Create new tmux session (detached)
- tmux new-session -d -s "$SESSION"
- # Split window horizontally: bottom pane for journalctl
- tmux split-window -v -t "$SESSION"
- # In bottom pane: run journalctl live
- tmux send-keys -t "$SESSION:0.1" "journalctl -fk -o short-iso" C-m
- # In top pane: run loop that cycles through each cpu using stress-ng
- tmux send-keys -t "$SESSION:0.0" "$(cat <<'EOF'
- cleanup() {
- echo "Interrupted! Killing stress-ng and exiting..."
- [[ -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
- COLOR="\e[94m::\e[0m" # Bright Blue.
- NUM_CPUS=$(nproc)
- SECONDS_PER_TEST=10 # 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).
- TOTAL_TIME=$((NUM_CPUS * (SECONDS_PER_TEST + SECONDS_BETWEEN_TESTS)))
- while true; do
- # Loop through all CPUs, stress-testing each sequentially.
- for cpu in $(seq 0 $((NUM_CPUS - 1))); do
- clear # NOTE: Comment out this line if you want to retain the output from past tests instead of clearing it.
- TIME_ELAPSED=$((cpu * (SECONDS_PER_TEST + SECONDS_BETWEEN_TESTS)))
- TIME_REMAINING=$((TOTAL_TIME - TIME_ELAPSED))
- PROGRESS=$(( (cpu + 1) * 100 / NUM_CPUS ))
- ETA=$(printf '%02d:%02d:%02d' $((TIME_REMAINING / 3600)) $(((TIME_REMAINING % 3600) / 60)) $((TIME_REMAINING % 60)))
- echo -e "${COLOR} Testing CPU $((cpu + 1)) of $NUM_CPUS [Progress: ${PROGRESS}% ETA: ${ETA}]\n"
- taskset -c "$cpu" stress-ng --cpu 1 --cpu-method all --metrics-brief --timeout ${SECONDS_PER_TEST}s &
- STRESS_PID=$!
- wait "${STRESS_PID}"
- unset STRESS_PID
- echo -e "\n${COLOR} Pausing for ${SECONDS_BETWEEN_TESTS} seconds before continuing...\n"
- sleep "${SECONDS_BETWEEN_TESTS}"
- done
- echo -e "${COLOR} All CPUs have been tested. Restarting test cycle in ${SECONDS_BETWEEN_TESTS} seconds..."
- sleep "${SECONDS_BETWEEN_TESTS}"
- done
- EOF
- )" C-m
- # Attach to tmux session
- tmux attach -t "$SESSION"
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
Advertisement