← til

Stopping the fans with a script

November 19, 2019

I find it irritating when fans on my laptop start spinning out of control, especially if it's caused by something non-critical or running in the background.

I caught myself going to htop regularly in order to find out what process is slamming the CPU and then SIGKILL it by pressing F9 and then 9.

That's why I decided to write a script that just kills the relevant process and therefore silences the fans. No htop necessary.

#! /usr/bin/env bash

# Silences the fans of your computer. Kills processes
# which use more than threshold percent
# of your CPU.

# The default threshold, processes using more than
# this percentage of the CPU will be killed with

# List PID, CPU percentage and command name.
ps -eo pid,%cpu,args |
  # Filter processes that are above the threshold.
  awk -v threshold=$threshold '{if($2==$2+0 && $2 > threshold) print $1 " " $3}' |
    # Sort by CPU usage to kill the worst offenders first.
    sort -n |
      # # Kill the passed process and report it being killed.
      xargs -L1 bash -c 'kill -9 $0 && printf "killed: $1\n"'

Put it in your $PATH and call it with

$ silencio

In order to go full Harry Potter on your fans.

@shime_sh Hrvoje Šimić
0 kudos