Using the 'top' command to troubleshoot your website

Overview

The top command allows you to view system tasks running in real-time. It provides a good summary of your system to quickly check if anything stands out that may be causing issues with your website or server. View the following man page for further details:

How do I run the 'top' command?

You must log into your server via SSH in order to run the top command. View the following articles for information on how to set up a Shell user and log into your server:

Shortcuts

top has a few shortcut keys that are available which changes how the information is displayed:

# Shortcut Key Description
c Toggles the COMMAND column between showing the command or program name.
t Displays summary information on and off.
m Displays memory information on and off.
A Sorts the display by top consumers of various system resources. Useful for quick identification of performance-hungry tasks on the system.
f Enters an interactive configuration screen for top. Helpful for setting up top for a specific task.
o Enables you to interactively select the ordering within top.
r Issues renice command.
k Issues kill command.
z Turns color/mono on and off.

This is the first step. Many times you can tell which site is causing trouble by simply checking your active processes. Log into your server via SSH and take a look at which processes are running. Let's say you see something like this:

[server]$ top -c
top - 14:37:35 up 10 days, 17:35,  3 users,  load average: 0.83, 0.89, 1.11
Tasks:  16 total,   1 running,  15 sleeping,   0 stopped,   0 zombie
Cpu(s): 34.7%us,  4.8%sy,  1.7%ni, 56.5%id,  0.9%wa,  0.2%hi,  1.1%si,  0.0%st
Mem:  32966092k total, 32546460k used,   419632k free,  6369232k buffers
Swap:  8000328k total,   228972k used,  7771356k free, 12650516k cached

PID   USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
8384  username  20   0 66984  11m 6852 S    1  0.0   0:03.62 php5.cgi
8385  username  20   0 66044  10m 6700 S    0  0.0   0:00.24 php5.cgi
10895 username  20   0 65940  10m 6848 S    0  0.0   0:00.92 php5.cgi
10917 username  20   0 65980  10m 6848 S    0  0.0   0:00.79 php5.cgi
7542  username  20   0 65956  10m 6860 S    0  0.0   0:00.51 php5.cgi
7818  username  20   0 65980  10m 6860 S    0  0.0   0:00.35 php5.cgi
7828  username  20   0 65988  10m 6860 S    0  0.0   0:00.33 php5.cgi
7917  username  20   0 66016  10m 6860 S    0  0.0   0:00.43 php5.cgi
8152  username  20   0 65976  10m 6856 S    0  0.0   0:04.21 php5.cgi 
8380  username  20   0 65932  10m 6848 S    0  0.0   0:04.03 php5.cgi
8386  username  20   0 66020  10m 6860 S    0  0.0   0:00.32 php5.cgi
10896 username  20   0 65908  10m 6848 S    0  0.0   0:00.66 php5.cgi
10919 username  20   0 65948  10m 6848 S    0  0.0   0:00.24 php5.cgi

If it looks something like the above, then you're very likely running into this problem. Generally, if you're running more than 10 PHP processes at once and they hold pretty steady, then this is an indication of memory issues.

This may help you diagnose a problem on a single site under your username, but if you have more than one site you should run lsof to find any open files that could potentially be causing an issue.

Sort results by memory usage

You can sort the results of top by memory usage. First, run the top command to view the live results.

Once opened, press Shift + m and the output is sorted by memory usage.

Displaying memory usage of a specific process

[server]$ top -b -n 1 | grep -i [EnterProcessName]

VPS and Dedicated Servers

If you're on a VPS or Dedicated Server, running top will display a lot of system processes run by users like 'root', 'dhapache', 'postfix', and so on. If you want to see only the processes running from a specific user, use the following command:

[server]$ top -U [EnterUsername]

See also

Did this article answer your questions?

Article last updated PST.

Still not finding what you're looking for?