Open Source

Configure IT Quick: Track Linux activity with TOP

Windows admins who love the Task Manager will be happy to learn that TOP can do many of the same things on Linux.

Even when your computer is just sitting there apparently doing nothing, there are lots of things going on in the background. In a Windows environment, when you want to see what your computer is doing and what processes are running in the background, you can run Task Manager. In Linux, you can display the same information by running TOP.

What's TOP?
TOP is a performance monitoring application that comes with most Linux distributions. It can display what processes are running, how much CPU time processes are taking, process IDs, and other information about what's going on on your workstation.

Like many Linux utilities, TOP is a command line utility. This may make it a little less user friendly, but you also have more control over it. By invoking some of the special switches in the command, you can customize the information that TOP displays.

Running TOP
To run TOP, open a terminal session on your Linux workstation. When the command prompt appears, type top and press [Enter]. When you do, you'll see a screen similar to Figure A.

Figure A
TOP tracks activity on your Linux workstation.

The first line of output displays the current time you've executed the command, the amount of time the workstation has been up, and the number of users on the system. Don't be surprised if you're the only one logged into your workstation and you see that TOP shows two users logged in. That's because some processes run as root, which count as separate users from the user ID you've logged in with.

The second line displays the number of processes running on the system. Processes represent programs running on the system. You'll notice that TOP breaks down processes into these areas:
  • Sleeping: Processes loaded into memory, but currently waiting to execute
  • Running: Processes currently executing
  • Zombie: Zombie processes are consuming memory on your system, but can't run or unload
  • Stopped: Stopped processes are those processes that you or the system have temporarily halted

The third line, CPU States, shows the percentage of CPU time the workstation is taking to run the process that it has loaded. This is also broken down into different areas:
  • User: CPU time spent on user programs such as Mozilla
  • System: CPU time spent on system programs
  • Nice: Amount of system time spent on programs whose priorities have been altered with the Nice command—Nice time is in addition to system time, so when you have nice time, the percentages may exceed 100 percent
  • Iowait: Amount of time spent processing IO tasks
  • Idle: Amount of extra processor time left in the CPU

The Mem line displays the amount of memory in the system and how it's used. Likewise, the Swap line displays the swap file usage. These lines tell you how much memory your system has, along with the amounts set aside for caching, buffering, and how much swap and regular memory are free in your system. You can also display this information with the Free command.

The bottom part of the TOP listing displays every process that's running on your system. By default, TOP displays the processes in CPU usage order. Processes consuming the most amount of CPU time appear at the top of the list. As TOP refreshes, processes jump up and down the list, reflecting their usage.

TOP breaks down processes into the following columns:
  • PID: Process ID, the identifier for the process
  • User: User ID that is running the process
  • PRI: Priority of the process
  • NI: The Nice value of the process—This value increases or decreases the priority of the process
  • Size: The size of the process's actual program, plus data and stack space
  • RSS: Total amount of physical memory used by the process
  • Share: Amount of shared memory used by the process
  • Stat: Displays the state of the process—Values can be N for a process with a positive Nice value, R for running, S for sleeping, T for stopped, W for swapped out, and/or Z for zombie; you may notice multiple values in this column
  • %CPU: Total percentage of CPU time spent on the process
  • %Mem: Total percentage of memory the process is using
  • Time: Total amount of time the process has been running
  • CPU: Which CPU the process is executing on—Unless you're running two or more CPUs on your system, this value will always be 0
  • Command: The name of the process

Fine-tuning TOP
As a command line utility, TOP includes some switches that you can invoke when you run the program that give you some added control. You can change the amount of interval time TOP takes between refreshes, display individual process IDs, and sort orders. Available switches in TOP include:
  • -s
    This changes the interval between updates. By default, TOP updates every five seconds. If you put in 0, TOP will update continuously, but be aware that this will skew your results.
  • -S
    This turns on cumulative mode, which will display the percentages divided over the entire monitoring time, rather than as a snapshot percentage. This will allow you track processes over time.
  • -h
    This displays the help for the TOP command.
  • -v
    This displays the current version of your TOP command. For the purposes of this article, I’m using TOP 2.0.11, which comes with RedHat 9.
  • -b
    This sorts the display by PID. Rather than refreshing the screen, TOP will completely scroll the output when invoked this way.
  • -c
    This switch displays the full path information in the Command column to show you where the process is being called from.
  • -i
    This switch suppresses idle processes. All you'll see is processes that are active.
  • -q
    This switch quits the program. You'll only use it as part of a script.
  • -d delay
    Delay is the amount of time you want to delay the display. Like -s, this switch modifies how often TOP updates.
  • -p pid
    Pid is the Process ID of a single process. This allows you to display only individual processes.
  • -n number
    Number is the number of times you want TOP to run. TOP will refresh itself the number of times you enter for number and then quit.

Depending on the version of Linux and/or the version of TOP you're running, you may have other switches at your disposal. You can view all of the switches by typing top –h and pressing [Enter].

Editor's Picks

Free Newsletters, In your Inbox