Last active
January 1, 2016 13:18
-
-
Save andrwj/8149934 to your computer and use it in GitHub Desktop.
set CPU limit to flash player!
This file contains 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
#!/bin/bash | |
# A.J <[email protected]> | |
# 2013-12-27 | |
# v0.0.6 | |
# | |
# set cpu limit to Flash player! | |
# default limit value: 30% | |
# usage: | |
# set-cpu-limit-to-flash-player.sh 50 | |
# | |
# NOTE: It assumes you play flash contents in Safari or Chrome. | |
# NOTE: `brew` should be on your system. | |
temporary_list_file="/tmp/$$.list" | |
previous_cpulimit_pid_file="/tmp/$$.dup" | |
webkitpluginhost_path="/System/Library/Frameworks/WebKit.framework/WebKitPluginHost.app/Contents/MacOS/WebKitPluginHost" | |
chromehelper_name="Google Chrome Helper" | |
limit=${1:-30} | |
cmd=/usr/local/bin/cpulimit | |
# don't left garbage .. | |
function clean_up { | |
rm -f $temporary_list_file | |
rm -f $previous_cpulimit_pid_file | |
echo " " | |
} | |
trap clean_up SIGHUP SIGINT SIGTERM | |
if [[ ! -e "$cmd" ]]; then | |
echo "- compile and install: cpulimit ..." | |
brew install cpulimit | |
if [[ $? -ne 0 ]]; then | |
echo | |
echo "- cpulimit is not presented. abort "; | |
echo | |
clean_up | |
fi | |
echo | |
fi | |
ps -ax | grep WebKitPluginHost | grep "$webkitpluginhost_path" > $temporary_list_file | |
ps -ax | grep "$chromehelper_name" | grep flash >> $temporary_list_file | |
while IFS=' ' read -r process_id tty _time path args; do | |
# damn ;-) | |
if [[ "$path" == "grep" ]]; then | |
continue; | |
fi | |
echo " " | |
rm -f $previous_cpulimit_pid_file | |
ps -ax | grep 'cpulimit' | grep "pid=$process_id" > $previous_cpulimit_pid_file | |
while IFS=' ' read -r previous_cpulimit_pid dummy dummy; do | |
if [[ ! -z $previous_cpulimit_pid ]]; then | |
echo -n "- Cancel previous cpulimit process($previous_cpulimit_pid) ..." | |
kill -TERM $previous_cpulimit_pid > /dev/null | |
if [ $? -eq 0 ]; then | |
echo " ok" | |
else | |
echo " failed. you should check out." | |
fi | |
fi | |
done <"$previous_cpulimit_pid_file" | |
$cmd --limit=$limit --pid=$process_id -z 2>&1 > /dev/null & | |
echo "=> Set cpu usage limitation '${limit}%' to the process($path)" | |
done <"$temporary_list_file" | |
clean_up |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment