16

I have a single core VPS with a load average that goes beyond 25 at times. When it reaches that, it becomes unbearably slow, and even commands run through dash, which is faster and uses less RAM than bash, takes a while to run. How can I track down what process is causing the high load?

2
  • 2
    sudo apt install htop gives you a useful text-mode tool for this purpose (more user friendly than the standard top). But top is good if you want a small foot-print.
    – sudodus
    Nov 5 '17 at 20:14
  • 2
    The load queue length indicates how many processes are ready to use CPU. This may not necessarily be a single process causing the problem. Nov 5 '17 at 22:45
22

You can install htop. Good thing about htop is that it will show you your usage per CPU, as well as a meaningful text graph of your memory and swap usage right at the top.

To install htop:

sudo apt-get install htop

Start it:

htop

enter image description here

Press F6 to sort the processes, then using the navigation key you can choose PERCENT_CPU and press enter.

enter image description here

Or you can use top in this way (source):

top -b -n 1 | head -n 12
2
  • 2
    As a shortcut, you can use P to sort by processor usage, M for memory, or T for time, and t to return to the tree layout.
    – deltab
    Nov 6 '17 at 3:53
  • 1
    Interestingly, htop has some basic mouse support. If you click on the columns, it will allow you to sort by them too (if I'm not mistaken). There are some other operations that work with your mouse. Nov 6 '17 at 13:27
11

The below is merely stolen from Unix.SE: Find the process which is taking maximum CPU usage if CPU usage is more than 60%?, though of course adapted to this question.

list processes by specific CPU usage

ps ahux --sort=-c | awk '{if($3>0.0)printf"%s %6d %s\n",$3,$2,$11}'

This gives a list of the processes which have a CPU usage >0.0%, you can change this value according to your needs, e.g. >50.0. Each line contains the CPU usage in percent, the PID and the file of the process.

list processes with the most CPU usage

ps ahux --sort=-c | awk 'NR<=5{printf"%s %6d %s\n",$3,$2,$11}'

This shows the top 5 (NR<=5) processes currently causing the most CPU load.

2

Yesterday I was studying awk and I played with the other two answers. Here is the result:

  • Get only the the process with the most higher CPU usage, using ps aux:

    ps auxh | awk -v max=0 '{if($3>max){CPU=$3; PID=$2; NAME=$11; max=$3}} END{printf "%5s %6d %s\n",CPU,PID,NAME}'
    
  • Get the three processes with the most higher CPU usage, using top:

    top -b -n 1 | awk 'NR>7 && NR<11 {printf "top: %5s %6d %s %s\n",$9,$1,$12,$13}'
    
  • Get the three processes with the most higher CPU usage, using ps aux:

    ps auxh --sort=-c | awk 'NR<=3 {printf "ps:  %5s %6d %s\n",$3,$2,$11}'
    

I've tried to run the last two commands simultaneously (with <command>; wait; <command> and <command> & <command> &), but then I've realised it is not possible at all :)


References:

3
  • The last one doesn't show the process with the most CPU usage, but starts with the second one, you probably meant NR>=2 there – turns out ps provides the option h which omits the header line, see my updated answer.
    – dessert
    Nov 6 '17 at 10:12
  • @dessert, you are right, previously it was NR>=2, but NR>1 is short :) I've updated the answer.
    – pa4080
    Nov 6 '17 at 10:21
  • Awesome. Thanks for top!
    – JavaRunner
    Jun 9 '20 at 7:56
2
ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem | head

ps -Flww -p %PID

just use the PIDs from the output of the first command and place it on the "%PID" place. Just using the man page:

To see every process with a user-defined format: ps -eo pid,tid,class,rtprio,ni,pri,psr,pcpu,stat,wchan:14,comm ps axo stat,euid,ruid,tty,tpgid,sess,pgrp,ppid,pid,pcpu,comm ps -Ao pid,tt,user,fname,tmout,f,wchan -l -l - Long format. The -y option is often useful with this. -F Extra full format. See the -f option, which -F implies. w Wide output. Use this option twice for unlimited width.

-p pidlist Select by PID. This selects the processes whose process ID numbers appear in pidlist. Identical to p and --pid.

3
  • 1
    Please add a explanation to the ps options Mar 20 at 13:02
  • can you add this explanation to your answer? Mar 22 at 7:50
  • 1
    Done as requested
    – Christiyan
    Mar 23 at 8:43
1

Use top command

top - display Linux processes

The top program provides a dynamic real-time view of a running system. It can display system summary information as well as a list of processes or threads currently being managed by the Linux kernel. The types of system summary information shown and the types, order and size of information displayed for processes are all user configurable and that configuration can be made persistent across restarts.

source

good youtube tutorial

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.