Created
March 13, 2018 16:46
-
-
Save jhyland87/9df6321f050de123563d7383fd836aa1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function stfups { | |
declare -A bundles | |
bundles[outlook]="com.microsoft.Outlook" | |
bundles[chrome]="com.google.Chrome" | |
bundles[vnc]="com.realvnc.vncviewer" | |
if [[ -z $1 ]]; then | |
echo "Need to specify a bundle - One of: ${!bundles[@]}" | |
return 1 | |
fi | |
bundle=${bundles[$1]} | |
cpulimit=${2:-"20"} | |
if test -z ${bundle}; then | |
echo "No bundle name found for ${1}" | |
return 1 | |
fi | |
pid=$(lsappinfo info ${bundle} -only pid | cut -d '=' -f2) | |
#ps aux | grep "[c]puthrottle ${pid}" | awk '{print \$2}' | xargs sudo kill -9 | |
if test -z $pid; then | |
echo "No PID found for bundle ${bundle}" | |
return 1 | |
fi | |
# Kill any existing cputhrottle commands for bundle | |
ps aux | grep -s "[c]puthrottle ${pid}" | |
if test $? == 0; then | |
echo "Killing existing cputhrottle limits" | |
ps aux | grep "[c]puthrottle ${pid}" | awk '{print $2}' | xargs sudo kill -9 | |
if test $? != 0; then | |
echo "Failed to kill existing cputhrottle processes" | |
return 1 | |
fi | |
if test ${cpulimit} == 0; then | |
echo "no limit" | |
else | |
echo "limit set" | |
fi | |
# If the cpulimit is 0, then just remove the throttle | |
test ${cpulimit} == 0 && return 0 | |
fi | |
echo "Throttling the CPU usage down for ${bundle} (PID ${pid}) - limit: ${cpulimit}" | |
sudo cputhrottle ${pid} 20 & disown | |
ret=$? | |
if test $? != 0; then | |
echo "Error - Return code: ${ret}" | |
return ${ret} | |
fi | |
echo "cputhrottle processes for ${pid}:" | |
ps aux | grep "[c]puthrottle ${pid}" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment