System Administration

Top Command in Linux

In UNIX-like systems, the top command reports valuable system information like running processes and resource usage. It shows processor activity and kernel-managed tasks in real-time. It’s one of the staple tools for system administrators.

In this guide, check out how to use the top command in Linux.

Top command usage

The top command will come pre-installed on any UNIX/Linux system. There’s no need to install any additional package to use it.

Check top version

Assuming your distro is up-to-date, your system should have the latest version of the top. Check the version of the top.

$ top -v

Default window

Run the top command without any parameter. This will load the default screen of the top.

$ top

It’ll output a real-time report of various information. Let’s have a quick breakdown of it.

The first heading portion reports hardware resource usage. The first line consists of the time, the amount of time the system is running, the number of logged-in users, and the load average. The second line reports the number of tasks along with their states.

Here’s a quick list of all the states. The value of each state describes how much time the CPU spends executing processes of that state.

  • us: Executing processes running under the userspace.
  • sy: Executing system kernel processes.
  • ni: Executing processes with a manually configured nice value.
  • id: The amount of time CPU remains idle.
  • wa: Waiting time for I/O to complete.
  • hi: Servicing hardware interrupts.
  • si: Servicing software interrupts.
  • st: Time lost for running virtual machines, also known as “steal time”.

The fourth line describes the system memory usage, for example, total physical memory amount and how much of it is used, free, buffered, or cached.

The second part of the output is a table listing all the running processes. Here’s a quick explanation of all the columns constituting the table. All these explain various attributes of the related process.

  • PID: The process ID of the process.
  • USER: The user the process is running under.
  • PR: Processor priority.
  • NI: Process nice value.
  • VIRT: Virtual memory used.
  • RES: Resident memory used.
  • SHR: Shared memory used.
  • S: Process status. It can of five types described below:
    • D: Uninterruptible sleep
    • R: Running
    • S: Sleeping
    • T: Traced
    • Z: Zombie
  • %CPU: CPU time consumed by the process.
  • %MEM: Percentage of physical memory used.
  • TIME+: Total CPU time used (in millisecond).
  • COMMAND: The command that’s the process is running with.

I guess that’s a lot of information to digest. It’s alright to take time and learn them slowly.

Scrolling

Top reports the entire list of running processes. As it’s a command-line tool, the navigation is a bit different than you’d expect in a GUI tool.

To navigate, use the Up and Down arrow keys. Additional navigation keys include Home, Page Up, Page Down, and End. To move the process list sideways, use the Left/Right arrow keys.

Change numeric unit

By default, the memory values are displayed in kibibytes. However, for practical purposes, it’s not a pleasant format. To switch the unit to other units, press “E”. The available unit formats are kibibytes, mebibytes, gibibytes, tebibytes, pebibytes, and exbibytes. Pressing “e” will do the same thing.

Change summary content

This impacts the first section of the top output. Press “l” to toggle the first line on/off.

If your system is running a multi-core CPU, then the top can report information for individual cores. Press “1” to toggle information on a per-core basis.

Want to change the CPU displays? Press “t”. On the first press, it’ll change the graphs to ASCII.

On the second press, it’ll change the graphs to solid block characters.

Press “t” once more and the CPU display and task summary will completely disappear.

To do the same with the memory and swap memory, press “m”.

Highlighting

The default screen of the top is just black and white. How about spicing things up? Press “z” to colorize the display.

To highlight the running tasks, press “y”. If you press “x”, the top will highlight the columns that it uses to sort the process list. The bold and reversed text highlight can be toggled by pressing “b”.

Full command line

In the case of running processes, we can toggle between just the process name and process name along with the full command. Press “c” to toggle.

To see a tree-style view where processes were launched/spawned by other processes, press “V”.

Filter output by the user

By default, the top will show the processes from all the users. To see the processes running under a specific process, press “u”. The top will ask for the user name or UID.

Active tasks

To see only the active tasks, press “I”.

To revert the changes, press “I” again.

Number of processes to display

By default, the top will report all the processes in a long table. However, we can manually set the number of processes top will display irrespective of the number of processes running. I think it’s a useful trick in very specific scenarios.

To limit the displays, press “n”. The top will ask for the number of processes to display. By default, the value is 0 (unlimited).

Change process priority

Every single running process will have a nice value to them. This nice value determines the priority of the process among all the running processes. The top allows manually defining the nice value.

To manually determine the nice value of a process, press “r”. The top will ask for the PID.

After entering the PID, the top will ask for the new nice value.

Kill process

In case you want to manually terminate a process, the top allows you to send a termination signal. To send a termination signal, we’ll be needing the PID and the signal name.

Press “k”. The top will ask for the PID.

Once the PID is entered, the top will then ask for the signal to send. By default, it’ll be the SIGTERM (kill) signal. Most of the time, it’s the signal you want to send. For a complete list of all the available signals, check out the signal man page.

$ man 7 signal

Miscellaneous shortcuts

Whatever changes you’ve made to how top behaves, it won’t be loaded unless you save it. To save the current preset, press “W”.

By default, the top will refresh the information at the default value. To define your custom value, press “d”.

To perform an instant quick refresh, press Spacebar.

Final thoughts

This is just the tip of the iceberg. The top is a complicated yet powerful tool for monitoring your system. There are plenty of tutorials available online. Check them out if you want to master the top command.

Interested in monitoring the system I/O? Then iotop is what you’re looking for. Check out how to monitor disk I/O using iotop.

Happy computing!

About the author

Sidratul Muntaha

Sidratul Muntaha

Student of CSE. I love Linux and playing with tech and gadgets. I use both Ubuntu and Linux Mint.