Linux – How to kill the top CPU/IO process quickly

killlinuxprocesstop

I find that sometimes my Linux will stop responding, so I need to switch to another terminal with CtrlAltF2, and type a command to kill the top CPU usage process or top IO usage process, so that the system can be responsive again.

This command should be short and execute quickly.
I know the command kill, but I do not know how to get the top-est process PID or name.

I will add this command into shell alias or function.

Best Answer

running top with batch mode via -b should get you the information you're looking for.

Here's a very messy start to what you could do:

top -b -n 1 | head | grep -A 1 PID | grep "^[0-9]" | cut -f1 -d" " | xargs kill

You can always kill a process from an interactive run of top using the k key as well, since you might not like what it picks...

Not sure what kernel you're running, but cgroups may also be of use to you in addition to limits.conf

Related Question