Skip to content

Instantly share code, notes, and snippets.

@yuchen
Forked from golimpio/egos_throttle.sh
Created July 5, 2024 02:46
Show Gist options
  • Select an option

  • Save yuchen/6b770e418e11a9e460c7c5f229bfeec3 to your computer and use it in GitHub Desktop.

Select an option

Save yuchen/6b770e418e11a9e460c7c5f229bfeec3 to your computer and use it in GitHub Desktop.

Revisions

  1. @golimpio golimpio revised this gist Mar 24, 2017. 1 changed file with 6 additions and 7 deletions.
    13 changes: 6 additions & 7 deletions egos_throttle.sh
    Original file line number Diff line number Diff line change
    @@ -17,7 +17,7 @@ if [ $1 = "--kill" ]; then
    echo "> Killing PID ${pid}"
    sudo kill ${pid}
    done
    echo "Done!"
    printf "Done!\n\n"
    exit 0
    fi

    @@ -35,7 +35,7 @@ set_cpu_limit() {
    service_cpu=$(ps aux | grep "sudo cputhrottle $pid $cpu_limit" | grep -v grep | wc -l)
    if [[ ! $service_cpu -gt 0 ]]; then
    sudo cputhrottle $pid $cpu_limit &
    printf " [started]\n"
    printf "\n"
    else
    printf " [already running]\n"
    fi
    @@ -44,17 +44,16 @@ set_cpu_limit() {
    declare -a applications

    # Syntax='application-name;max-cpu%(1-100)'
    applications[0]='Chrome;40'
    applications[1]='idea;50'
    applications[0]='Chrome;50'
    applications[1]='idea;65'
    applications[2]='pycharm;40'
    applications[3]='webstorm;40'
    applications[4]='datagrip;35'
    applications[4]='datagrip;40'

    for i in "${applications[@]}"; do
    app=(${i//;/ })
    app_name=${app[0]}
    cpu_limit=${app[1]}

    printf "\nLooking for ${app_name}...\n"
    pids=`pidof ${app}`
    for pid in ${pids}; do
    @@ -63,4 +62,4 @@ for i in "${applications[@]}"; do
    done

    printf "\nDone!\n"
    echo "Run this script passing '--kill' as argument to remove all cputhrottles."
    printf "Run this script passing '--kill' as argument to remove all cputhrottles.\n\n"
  2. @golimpio golimpio revised this gist Mar 23, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion egos_throttle.sh
    Original file line number Diff line number Diff line change
    @@ -43,7 +43,7 @@ set_cpu_limit() {

    declare -a applications

    # Syntax='application;max-cpu'
    # Syntax='application-name;max-cpu%(1-100)'
    applications[0]='Chrome;40'
    applications[1]='idea;50'
    applications[2]='pycharm;40'
  3. @golimpio golimpio revised this gist Mar 23, 2017. 1 changed file with 21 additions and 2 deletions.
    23 changes: 21 additions & 2 deletions egos_throttle.sh
    Original file line number Diff line number Diff line change
    @@ -21,6 +21,26 @@ if [ $1 = "--kill" ]; then
    exit 0
    fi

    # Start cputhrottle if it's not running yet for the given PID
    set_cpu_limit() {
    pid=$1
    cpu_limit=$2
    if [[ "$pid" == "" || "$cpu_limit" == "" || $cpu_limit -lt 1 || $cpu_limit -gt 100 ]]; then
    echo "! Invalid arguments: pid=$pid, cpu_limit=$cpu_limit"
    return
    fi

    printf "> PID=${pid}, CPU=${cpu_limit}"

    service_cpu=$(ps aux | grep "sudo cputhrottle $pid $cpu_limit" | grep -v grep | wc -l)
    if [[ ! $service_cpu -gt 0 ]]; then
    sudo cputhrottle $pid $cpu_limit &
    printf " [started]\n"
    else
    printf " [already running]\n"
    fi
    }

    declare -a applications

    # Syntax='application;max-cpu'
    @@ -38,8 +58,7 @@ for i in "${applications[@]}"; do
    printf "\nLooking for ${app_name}...\n"
    pids=`pidof ${app}`
    for pid in ${pids}; do
    echo "> PID=${pid}, CPU=${cpu_limit}"
    sudo cputhrottle ${pid} ${cpu_limit} &
    set_cpu_limit ${pid} ${cpu_limit}
    done
    done

  4. @golimpio golimpio revised this gist Mar 23, 2017. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions egos_throttle.sh
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,9 @@
    #!/bin/bash

    # Run cputhrottle for a list of applications in order to limit their CPU usage.
    # This script needs `pidof` and `cputhrottle` installed, which can be installed from homebrew.
    # NOTE: This script was tested on MacOS only.

    if [[ $EUID > 0 ]]; then
    echo "Please run this script as root/sudo"
    exit 1
  5. @golimpio golimpio created this gist Mar 23, 2017.
    43 changes: 43 additions & 0 deletions egos_throttle.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@
    #!/bin/bash

    if [[ $EUID > 0 ]]; then
    echo "Please run this script as root/sudo"
    exit 1
    fi

    # Pass --kill as argument to kill all running cputhrottles
    if [ $1 = "--kill" ]; then
    echo "Looking for running cputhrottles..."
    pids=`pidof cputhrottle`
    for pid in ${pids}; do
    echo "> Killing PID ${pid}"
    sudo kill ${pid}
    done
    echo "Done!"
    exit 0
    fi

    declare -a applications

    # Syntax='application;max-cpu'
    applications[0]='Chrome;40'
    applications[1]='idea;50'
    applications[2]='pycharm;40'
    applications[3]='webstorm;40'
    applications[4]='datagrip;35'

    for i in "${applications[@]}"; do
    app=(${i//;/ })
    app_name=${app[0]}
    cpu_limit=${app[1]}

    printf "\nLooking for ${app_name}...\n"
    pids=`pidof ${app}`
    for pid in ${pids}; do
    echo "> PID=${pid}, CPU=${cpu_limit}"
    sudo cputhrottle ${pid} ${cpu_limit} &
    done
    done

    printf "\nDone!\n"
    echo "Run this script passing '--kill' as argument to remove all cputhrottles."