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 osx, 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}')
(Not Responding)appended to the name.