Find Process with High Memory/CPU Linux

ps aux | head output: the USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND header and the root /sbin/init line

Sometimes in Linux environments, especially shared servers, the top command does not really give you the full picture of what is going on. This guide shows how to use ps, awk and sort to find the process (and the user) taking the CPU or memory. It was written on CentOS 6 and is republished from the original blog with its screenshots; notes for current AlmaLinux, Rocky Linux and RHEL releases are at the end.

For example, if you have a dozen apps running on a server, and they each have a process, and each is running, top won’t be as incredibly useful as you may think. What process is taking all the memory or CPU? There’s 100’s, maybe 1000’s.

List the processes with ps aux

This can be done as a standard user, login with your local/LDAP account.

bash-4.1 shell prompt, logged in as a standard user

from here issue the following command:

ps aux | head
ps aux | head typed at the bash prompt

this is the basis of the command we are going to be using, it’s important to find the field that you need to look for.

ps aux | head output: the USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND header and the root /sbin/init line

so in this case, columns 2,3,4 (PID, CPU, and MEMORY) are quite useful to know ahead of time.

Sort the processes by CPU with awk and sort

In this example we are looking for something using high CPU.

You could scroll through the list using “more”, however if you have 100’s or 1000’s of processes, it will not be quick.

ps aux | more
ps aux | more typed at the bash prompt

here’s a bit more refined command:

ps aux | awk -F " " '{print $3 " " $1 }' | sort
ps aux | awk -F " " '{print $3 " " $1 }' | sort, ending with 99.1 andrew and the %CPU USER header

so:

  • ps aux prints all processes.
  • awk -F makes the field separator a space ” “, so from there we print the value after the 3rd space (CPU)
  • the ” ” prints a space in-between the values, $1 for the user, and then sort to sort the output in order.

We have our culprit:

Sorted output ending with 99.4 andrew and the %CPU USER header

Add the process ID

now we want to get the process from this, add $2 to get the process ID:

ps aux | awk -F " " '{print $3 " " $1 " " $2}' | sort
ps aux | awk -F " " '{print $3 " " $1 " " $2}' | sort typed at the bash prompt

at this point we know what the troubled process is, and the ID running the process.

Output 99.3 andrew 13602 and the %CPU USER PID header

Check the threads of the process

from here you may want to check the number of threads (sub-processes) opened by the process.

ps -eLf | grep 13602
ps -eLf | grep 13602 showing one thread of ./OneCPU owned by andrew, PID 13602

in this case there is only one, but each thread could be taking up memory/CPU, so it’s important to know.

From here you can kill the process or contact the developer, and let them know their app is messed up.

Memory instead of CPU

The same command works for memory: %MEM is field 4, so print $4 instead of $3:

ps aux | awk -F " " '{print $4 " " $1 " " $2}' | sort -n | tail

On AlmaLinux, Rocky Linux and RHEL 8, 9 and 10

  • Plain sort compares text, so a process at 9.5% sorts after one at 10.2%. Add -n (sort -n) to sort numerically, and tail to see only the top entries.
  • The procps-ng ps can sort for you, without awk: ps aux --sort=-%cpu | head for CPU, ps aux --sort=-%mem | head for memory, or choose columns with ps -eo pid,user,%cpu,%mem,rss,comm --sort=-%mem | head.
  • Interactively, top sorts by memory with Shift+M and by CPU with Shift+P, or start it with top -o %MEM.
  • For the thread count of one process, ps -o nlwp= -p 13602 prints just the number, and ps -L -p 13602 lists the threads.
  • If the process belongs to a systemd service, systemctl status <pid> shows which unit it is, and systemd-cgtop shows CPU and memory per service.
  • Prefer kill (SIGTERM) before kill -9, and restart a service with systemctl restart rather than killing it.

That’s it!

Hosted Linux Servers at www.zwiegnet.com/go

Related guides: Find Processor Cores Linux | Solaris Process Management: ps -ef, pgrep, pkill, kill Signals and nohup

Leave a Reply

Your email address will not be published. Required fields are marked *