Guest User

Untitled

a guest
Jun 18th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Run cputhrottle for a list of applications in order to limit their CPU usage.
  4. # This script needs `pidof` and `cputhrottle` installed, which can be installed from homebrew.
  5. # NOTE: This script was tested on MacOS only.
  6.  
  7. if [[ $EUID > 0 ]]; then
  8. echo "Please run this script as root/sudo"
  9. exit 1
  10. fi
  11.  
  12. # Pass --kill as argument to kill all running cputhrottles
  13. if [ $1 = "--kill" ]; then
  14. echo "Looking for running cputhrottles..."
  15. pids=`pidof cputhrottle`
  16. for pid in ${pids}; do
  17. echo "> Killing PID ${pid}"
  18. sudo kill ${pid}
  19. done
  20. printf "Done!\n\n"
  21. exit 0
  22. fi
  23.  
  24. # Start cputhrottle if it's not running yet for the given PID
  25. set_cpu_limit() {
  26. pid=$1
  27. cpu_limit=$2
  28. if [[ "$pid" == "" || "$cpu_limit" == "" || $cpu_limit -lt 1 || $cpu_limit -gt 100 ]]; then
  29. echo "! Invalid arguments: pid=$pid, cpu_limit=$cpu_limit"
  30. return
  31. fi
  32.  
  33. printf "> PID=${pid}, CPU=${cpu_limit}"
  34.  
  35. service_cpu=$(ps aux | grep "sudo cputhrottle $pid $cpu_limit" | grep -v grep | wc -l)
  36. if [[ ! $service_cpu -gt 0 ]]; then
  37. sudo cputhrottle $pid $cpu_limit &
  38. printf "\n"
  39. else
  40. printf " [already running]\n"
  41. fi
  42. }
  43.  
  44. declare -a applications
  45.  
  46. # Syntax='application-name;max-cpu%(1-100)'
  47. applications[0]='Chrome;50'
  48. applications[1]='idea;65'
  49. applications[2]='pycharm;40'
  50. applications[3]='webstorm;40'
  51. applications[4]='datagrip;40'
  52.  
  53. for i in "${applications[@]}"; do
  54. app=(${i//;/ })
  55. app_name=${app[0]}
  56. cpu_limit=${app[1]}
  57. printf "\nLooking for ${app_name}...\n"
  58. pids=`pidof ${app}`
  59. for pid in ${pids}; do
  60. set_cpu_limit ${pid} ${cpu_limit}
  61. done
  62. done
  63.  
  64. printf "\nDone!\n"
  65. printf "Run this script passing '--kill' as argument to remove all cputhrottles.\n\n"
Add Comment
Please, Sign In to add comment