Advertisement
FocusedWolf

Arch: CPU stress test script with tmux split view of live journalctl events

Jul 12th, 2025 (edited)
527
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.71 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Version 7
  4.  
  5. # Installation:
  6. #     $ sudo cp ./testcpu /usr/local/bin/
  7. #     $ sudo chmod +x /usr/local/bin/testcpu
  8.  
  9. # This script runs 'stress-ng' on each CPU one at a time,
  10. # while showing live system logs via journalctl in a split tmux session.
  11. #
  12. # Usage:
  13. #   1. Ensure 'tmux' and 'stress-ng' are installed: $ sudo pacman -S tmux stress-ng
  14. #
  15. #   2. Run the script with: $ testcpu
  16. #
  17. #   3. This script will launch a tmux session named 'cpu_test' with two panes:
  18. #      The top pane runs the stress test.
  19. #      The bottom pane displays live journalctl events.
  20. #      NOTE: This script runs continuously until manually stopped.
  21. #
  22. #   4. To stop this script:
  23. #      From inside tmux: press Ctrl+C in each pane and run $ exit
  24. #      From outside tmux: $ sudo tmux kill-session -t cpu_test
  25. #      NOTE: If tmux or stress-ng are still running then terminate them using System Monitor or any process manager you prefer.
  26.  
  27. # If tmux is not installed.
  28. if ! command -v tmux >/dev/null 2>&1; then
  29.     echo -e "\e[94m::\e[0m tmux could not be found. Install with: $ sudo pacman -S tmux"
  30.     exit 1
  31. fi
  32.  
  33. # If stress-ng is not installed.
  34. if ! command -v stress-ng >/dev/null 2>&1; then
  35.     echo -e "\e[94m::\e[0m stress-ng could not be found. Install with: $ sudo pacman -S stress-ng"
  36.     exit 1
  37. fi
  38.  
  39. # Check if script is running as root, if not re-run with sudo
  40. if [[ $EUID -ne 0 ]]; then
  41.     echo "Requesting administrative privileges..."
  42.     exec sudo "$0" "$@"
  43. fi
  44.  
  45. SESSION="cpu_test"
  46.  
  47. # Number of logical CPUs
  48. NUM_CPUS=$(nproc)
  49.  
  50. # Create new tmux session (detached)
  51. tmux new-session -d -s "$SESSION"
  52.  
  53. # Split window horizontally: bottom pane for journalctl
  54. tmux split-window -v -t "$SESSION"
  55.  
  56. # In bottom pane: run journalctl live
  57. tmux send-keys -t "$SESSION:0.1" "journalctl -fk -o short-iso" C-m
  58.  
  59. # In top pane: run loop that cycles through each cpu using stress-ng
  60. tmux send-keys -t "$SESSION:0.0" "$(cat <<'EOF'
  61. cleanup() {
  62.    echo "Interrupted! Killing stress-ng and exiting..."
  63.    [[ -n "${STRESS_PID}" && "${STRESS_PID}" =~ ^[0-9]+$ ]] && kill "${STRESS_PID}" 2>/dev/null
  64.    exit 1
  65. }
  66.  
  67. # Run cleanup() function on script interruption or termination.
  68. trap cleanup SIGINT SIGTERM SIGHUP
  69.  
  70. COLOR="\e[94m::\e[0m" # Bright Blue.
  71. NUM_CPUS=$(nproc)
  72. SECONDS_PER_TEST=10 # The duration of the cpu tests.
  73. SECONDS_BETWEEN_TESTS=3 # The delay between cpu tests (gives time to read the output and for the processor to cool).
  74. TOTAL_TIME=$((NUM_CPUS * (SECONDS_PER_TEST + SECONDS_BETWEEN_TESTS)))
  75. while true; do
  76.    # Loop through all CPUs, stress-testing each sequentially.
  77.    for cpu in $(seq 0 $((NUM_CPUS - 1))); do
  78.        clear # NOTE: Comment out this line if you want to retain the output from past tests instead of clearing it.
  79.  
  80.        TIME_ELAPSED=$((cpu * (SECONDS_PER_TEST + SECONDS_BETWEEN_TESTS)))
  81.        TIME_REMAINING=$((TOTAL_TIME - TIME_ELAPSED))
  82.        PROGRESS=$(( (cpu + 1) * 100 / NUM_CPUS ))
  83.        ETA=$(printf '%02d:%02d:%02d' $((TIME_REMAINING / 3600)) $(((TIME_REMAINING % 3600) / 60)) $((TIME_REMAINING % 60)))
  84.        echo -e "${COLOR} Testing CPU $((cpu + 1)) of $NUM_CPUS [Progress: ${PROGRESS}% ETA: ${ETA}]\n"
  85.  
  86.        taskset -c "$cpu" stress-ng --cpu 1 --cpu-method all --metrics-brief --timeout ${SECONDS_PER_TEST}s &
  87.        STRESS_PID=$!
  88.        wait "${STRESS_PID}"
  89.        unset STRESS_PID
  90.  
  91.        echo -e "\n${COLOR} Pausing for ${SECONDS_BETWEEN_TESTS} seconds before continuing...\n"
  92.        sleep "${SECONDS_BETWEEN_TESTS}"
  93.    done
  94.  
  95.    echo -e "${COLOR} All CPUs have been tested. Restarting test cycle in ${SECONDS_BETWEEN_TESTS} seconds..."
  96.    sleep "${SECONDS_BETWEEN_TESTS}"
  97. done
  98. EOF
  99. )" C-m
  100.  
  101. # Attach to tmux session
  102. tmux attach -t "$SESSION"
Advertisement
Comments
  • # text 0.38 KB | 0 0
    1. 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/
    2.  
Add Comment
Please, Sign In to add comment
Advertisement