The basic form of the command – what you see when you simply type “top” – shows the most useful performance statistics you’re likely to find on a Unix system. You get a view of how much memory is being used, the system load, the processes that are using the most resources (and who is running those processes), how long the system has been up, and whether any serious swapping is going on.
Take the following top output. This system has been running for over a year since its last reboot. Some of the numbers that quickly tell you that the system is not having any problems include:
You’ll probably notice that top just happens to be the process that is listed first. It's almost always a sign that the system doesn’t have much to do if the top command gets more access to the CPU than other processes.
This command updates the display every 3 seconds, shows you additional lines of process data if you stretch out your window, and runs until you type “q” for quit or ^c.
That first line, as you might have noticed, is the same that you get from the uptime command.
The load averages are one of the most useful measures for gauging overall system performance. These three numbers represent the number (on average) number of processes that are having to wait to get access to the CPU. If the load average were 1, it would mean that there’s generally a process waiting all of the time. The .025 measurement means that there’s a process waiting only once in every four times that number is queried.
There are three numbers – one that shows the average over the last minute, one that shows the average over the last five minutes, and one that shows the number over the last fifteen minutes.
Getting a sense of whether the system is slowing down or speeding up, therefore, only takes a quick look at the three load statistics. In this case, the system is slowing down. You can tell this because the one-minute average is larger than the five-minute statistic and the five-minute average is larger than the fifteen-minute statistic. Keep in mind, however, that we’re only looking at fifteen minutes worth of data. You’ll need to capture a lot more load data to get a feel for how the system is doing over a longer period of time.
The top command’s default is to organize the processes it report by CPU usage (highest usage first). However, you can change that interactively or when you start:
Press T if you want to sort by the time processes have been running (longest running first).
You can also change the interval that top uses -- the number of seconds that it waits between display updates. Use the –d option followed by the number of seconds you want each interval to take. Note that you can specify portions of seconds if you’re so inclined, though .05 is going to produce quite a frenetic display.
And you can control how many views you see -- a continuous stream or some specific number of updates. Add –n # (e.g., -n 10) if you want to see only a specific number of updates before your top command completes.
The command below shows performance data organized by memory usage with only one view.
To look at one specific user, use the –u option.
You can show cumulative time (how long processes and their dead child processes have been running) using the -S option.
You can actually terminate a process within top if you have sufficient privilege. To do that, type k. You will then be prompted to enter the process ID. Similarly, you can change the nice setting of a process by typing r. You will be prompted to enter the process ID and the nice value. Setting a "nice" value, by the way, is sort of like ordering something from Amazon Prime and saying you don’t need it in two days. You're lowering the process' priority to be "nice" to other processes, giving them more opportunities to run. Some processes run with a "nice" value by default and you'll spot these when using top.
One other interesting aspect of top is that you can save your top settings by pressing W (for write) when you're running top. This will store your settings in a file called .toprc (~/.toprc). If you want to change your default top settings, you can try editing that file. For example, you can change the interval between updates (look for Delay_time in the output below) or the order in which top's columns are displayed (reverse the fieldscur settings). On the other hand, making these changes interactively is easier and more flexible.
And, of course, you can always turn your favorite top command variations into aliases to make them easier to remember and use.