You may want to go one step further, and automate the process:
- Auto-detect processes (which can be running in browser tabs) which are hogging your CPU
- Terminate them if/once they exceed certain/unreasonable thresholds
Here's a generic/customizable script I wrote, called
cpu-hog-killer to address this issue of malicious or otherwise misbehaving scripts in browsers (or elsewhere).
The script defines two basic primitives:
process-list
: pick processes based on a command+args regex
terminate-hogs
: kill processes exceeding desired CPU (total-seconds & current %pct) consumption
and uses them to achieve this goal.
Here's the description, as taken from the README.md in the git repo.
cpu-hog-killer
Kill high-CPU hogging processes by command+args regex & CPU consumption.
Buggy (or malicious) 3rd-party javascript code is often taking
our browsers and computing resources hostage.
I used to find some processes on my desktop running at 100% CPU
all night when I was not even noticing, because of bad javascript.
Involuntary CPU hogging might be caused by an infected ad,
badly written code, "drive-by-scripting" leading to someone
co-opting your computer to run crypto-mining, or much worse.
It makes you pay for electricity you don't want to, and multiplied
by many desktops and browsers left running at night, is also very bad
for our planet.
cpu-hog-killer
is a simple script which I run from periodically
during the night. I make it run from cron every 13 minutes like this:
# Add this line (or similar) using 'crontab -e'
*/13 0-7 * * * ~/bin/cpu-hog-killer
It identifies certain CPU hogging processes, mainly inside Chrome or Firefox, and kills them instantly upon detection.
Note that it doesn't kill processes taking CPU indiscriminately, nor
it kills the main browser process.
It only kills those processes that you explicitly say you want
terminated based on configured regex
+ CPU consumption parameters
.
In the morning when I'm back, the worst case scenario is that
I go to a browser tab and it says (e.g. for Firefox):
"Gah. Your tab just crashed."
I take notice of which site was misbehaving (based on its title)
and the browser allows me (presenting a big button) to:
[Restore This Tab]
if I want to, so nothing is actually lost.
You can add more rules to the script to cover more apps that
may be hogging your CPU when you don't want them to. Just look
at the 'main' section (last few lines of the script) and add
more rules as needed. Each rule looks like this:
# [max_cpu_secs] [max_cpu_pct] are optional parameters to terminate-hogs
# if not specified, default values will be used
process-list '<some pattern>' | terminate-hogs [max_cpu_secs] [max_cpu_pct]
For example, the Firefox rule which only kills one process tab is:
process-list '[/]firefox -contentproc -childID.*tab$' | terminate-hogs
And for Chrome/Chromium it is:
process-list '[/]chrom(e|ium) --type=(renderer|utility)' | terminate-hogs