Created
December 5, 2016 19:03
-
-
Save Geesu/018fcc388e0f377e6bdca312ab817b90 to your computer and use it in GitHub Desktop.
Limit VTDecoderXPCService using cputhrottle
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
#!/usr/bin/env ruby | |
# Configure the below | |
process_name = 'VTDecoderXPCService' | |
cpu_percentage = 10 | |
raise 'Must run as root' unless Process.uid == 0 | |
restricted_pids = [] | |
throttle_pids = [] | |
begin | |
loop do | |
pids = `pgrep "#{process_name}"` | |
if pids | |
pids = pids.split("\n") | |
pids.each do |pid| | |
next if restricted_pids.include? pid | |
puts "Restricting process ID #{pid}" | |
restricted_pids << pid | |
throttle_pids << Process.spawn("/usr/local/bin/cputhrottle #{pid} #{cpu_percentage}") | |
end | |
end | |
sleep 1 | |
end | |
rescue SystemExit, Interrupt | |
throttle_pids.each do |pid| | |
begin | |
puts "Killing cputhrottle process #{pid}" | |
Process.kill('QUIT', pid) | |
rescue Errno::ESRCH | |
# process exited normally | |
end | |
end | |
raise | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment