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?
5 Answers
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 htopStart it:
htopPress F6 to sort the processes, then using the navigation key you can choose PERCENT_CPU and press enter.
Or you can use top in this way (source):
top -b -n 1 | head -n 12 2 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.
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:
- The other two nice answers from the current question (and this comment of @αғsнιη).
- Find the max value of column 1 and print respective record from column 2 from file.
- How to run awk for some number of lines?
ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem | head
ps -Flww -p %PIDjust 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.
3Use top command
top- display Linux processesThe 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.