1

For some reason, I end up with a fair few Google Chrome Helper processes which stop responding, hogging CPU resources. Usually I just run killall Google\ Chrome\ Helper, however this kills all, including the responsive processes meaning that I have to restart Chrome.

Is there a flag for killall so it only affects unresponsive processes?

4
  • 2
    How will you identify "unresponsiveness"?
    – mailq
    Oct 10, 2014 at 12:12
  • Unfortunately it seems this issue had been going for a long time.
    – Ramesh
    Oct 10, 2014 at 14:35
  • If you open up Activity Monitor on Mac, you'll see unresponsive processes in red with (Not Responding) appended to the name. Oct 10, 2014 at 16:26
  • Related answer to fully automate killing of misbehaving firefox/chrome browser tabs: unix.stackexchange.com/questions/484388/…
    – arielf
    Dec 26, 2022 at 23:00

1 Answer 1

0

One way to do it is to use top to find the pid of the process using the most CPU. I started a bash CPU hog in one terminal:

bash -c "while true; do :; done"

Then in another terminal I can kill it as follows:

kill $( top -l2 | grep bash | sort -nrk3 | awk '{print $1;exit}' )

Note, since this is , this is the BSD top and not the GNU version.

  • -l2 tells top to run for 2 iterations - the first needs to be ignored as it just reports 0% CPU for all processes.
  • The grep filters just the bash lines. Note this may need more work if your grep expression matches any other parts of the top output.
  • sort sorts the output numerically in reverse by the 3rd column (CPU %)
  • head gets the first line (highest CPU)
  • cut gets the first column (PID)
  • The above is executed in a $() command substitution, and the numerical PID is just passed directly to kill

On GNU/Linux machines the equivalent is:

kill $(top -bn1 | grep bash | sort -nrk9 | awk '{print $1;exit}')

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .