Using the 'top' command to troubleshoot your website

 

Overview

This article explains how to use the top command to view system tasks running in real-time on your web server. This can help to troubleshooting issues with your website or server. See this page for further details on this tool.

Prerequisites

This article assumes you have created a shell user and are able to log into your server via SSH.

Using the 'top' command

The following sections provide different ways to use this command. 

Running the command

Log into your server via SSH and run the top -c command to view the current processes running. For example:

[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

If you see many PHP processes similar to the above output, you may likely be running into memory issues. You can continue troubleshooting using the shortcuts below.

Shortcuts

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

# Shortcut Key Description
Shift + m Sort output by memory usage.
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.

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', and 'postfix'. If you want to see only the processes running from a specific user, type in the shell username after the command:

[server]$ top -U [Username]

See also

Did this article answer your questions?

Article last updated PST.

Still not finding what you're looking for?