scaling-governor.sh 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #!/usr/bin/env bash
  2. # Copyright (c) 2021-2023 tteck
  3. # Author: tteck (tteckster)
  4. # License: MIT
  5. # https://github.com/tteck/Proxmox/raw/main/LICENSE
  6. set -e
  7. header_info() {
  8. clear
  9. cat <<EOF
  10. ________ __ __ _____
  11. / ___/ _ \/ / / / / ___/__ _ _____ _______ ___ _______
  12. / /__/ ___/ /_/ / / (_ / _ \ |/ / -_) __/ _ \/ _ \/ __(_-<
  13. \___/_/ \____/ \___/\___/___/\__/_/ /_//_/\___/_/ /___/
  14. EOF
  15. }
  16. header_info
  17. whiptail --backtitle "Proxmox VE Helper Scripts" --title "CPU Scaling Governors" --yesno "View/Change CPU Scaling Governors. Proceed?" 10 58 || exit
  18. current_governor=$(cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor)
  19. GOVERNORS_MENU=()
  20. MSG_MAX_LENGTH=0
  21. while read -r TAG ITEM; do
  22. OFFSET=2
  23. ((${#ITEM} + OFFSET > MSG_MAX_LENGTH)) && MSG_MAX_LENGTH=${#ITEM}+OFFSET
  24. GOVERNORS_MENU+=("$TAG" "$ITEM " "OFF")
  25. done < <(cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors | tr ' ' '\n' | grep -v "$current_governor")
  26. scaling_governor=$(whiptail --backtitle "Proxmox VE Helper Scripts" --title "Current CPU Scaling Governor is set to $current_governor" --checklist "\nSelect the Scaling Governor to use:\n" 16 $((MSG_MAX_LENGTH + 58)) 6 "${GOVERNORS_MENU[@]}" 3>&1 1>&2 2>&3 | tr -d '"') || exit
  27. [ -z "$scaling_governor" ] && {
  28. whiptail --backtitle "Proxmox VE Helper Scripts" --title "No CPU Scaling Governor Selected" --msgbox "It appears that no CPU Scaling Governor was selected" 10 68
  29. clear
  30. exit
  31. }
  32. echo "${scaling_governor}" | tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor >/dev/null
  33. current_governor=$(cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor)
  34. whiptail --backtitle "Proxmox VE Helper Scripts" --msgbox --title "Current CPU Scaling Governor" "\nCurrent CPU Scaling Governor has been set to $current_governor\n" 10 60
  35. CHOICE=$(whiptail --backtitle "Proxmox VE Helper Scripts" --title "CPU Scaling Governor" --menu "This will establish a crontab to maintain the CPU Scaling Governor configuration across reboots.\n \nSetup a crontab?" 14 68 2 \
  36. "yes" " " \
  37. "no" " " 3>&2 2>&1 1>&3)
  38. case $CHOICE in
  39. yes)
  40. set +e
  41. NEW_CRONTAB_COMMAND="(sleep 60 && echo \"$current_governor\" | tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor)"
  42. EXISTING_CRONTAB=$(crontab -l 2>/dev/null)
  43. if [[ -n "$EXISTING_CRONTAB" ]]; then
  44. TEMP_CRONTAB_FILE=$(mktemp)
  45. echo "$EXISTING_CRONTAB" | grep -v "@reboot (sleep 60 && echo*" > "$TEMP_CRONTAB_FILE"
  46. crontab "$TEMP_CRONTAB_FILE"
  47. rm "$TEMP_CRONTAB_FILE"
  48. fi
  49. (crontab -l 2>/dev/null; echo "@reboot $NEW_CRONTAB_COMMAND") | crontab -
  50. echo -e "\nCrontab Set (use 'crontab -e' to check)"
  51. ;;
  52. no)
  53. echo -e "\n\033[31mNOTE: Settings return to default after reboot\033[m\n"
  54. ;;
  55. esac
  56. echo -e "Current CPU Scaling Governor is set to \033[36m$current_governor\033[m\n"