Skip to content

Mastering the Linux top Command: Monitoring and Managing Processes

As a Linux system administrator or power user, it‘s crucial to understand how to monitor the health and resource usage of your system. One of the most fundamental tools for this is the venerable top command. More than just a process viewer, top allows you to interactively monitor and manage the processes running on your Linux system in real-time.

In this in-depth guide, we‘ll walk through everything you need to know to master the top command. We‘ll explain what information top provides, show how to interpret its output, and demonstrate its interactive features for managing processes – including the ability to change a process‘s "nice value" or priority. Let‘s dive in!

What is the top command?

The top command is a real-time system monitor that displays a list of processes currently running on your Linux system, along with a summary of system resource usage. When launched, top presents this information in two main sections:

  1. A system summary area at the top
  2. A process list that updates continuously

The system summary shows high-level statistics for CPU usage, memory usage, system load, number of running processes, and more. The process list displays data about each process including its PID, owner, CPU and memory utilization, and other key attributes.

Unlike other tools that provide a static snapshot, top updates its display every few seconds, allowing you to monitor your system in real-time. Even better, top is interactive – you can issue commands to change the display, sort the process list in different ways, and most powerfully, manage the processes themselves. This makes top an indispensable tool for any Linux admin.

Launching top and basic usage

Invoking top couldn‘t be simpler – just open a terminal and type top at the command prompt:

top

By default, top will launch and begin displaying the system summary and process list, refreshing every 3 seconds. To exit top and return to the shell prompt, press the q key.

While running, you can interact with top using single key presses. Some of the most useful basic commands include:

  • h – Displays a help screen with available interactive commands
  • spacebar – Immediately refreshes the display
  • M – Sorts the process list by memory usage
  • P – Sorts the process list by CPU usage
  • u – Displays only processes owned by a specific user

There are dozens of other interactive commands, which we‘ll explore further. But first, let‘s take a closer look at the wealth of information top presents and how to interpret it.

Decoding the top display

When you launch top, you‘re greeted with a dense screen of data that may seem overwhelming at first. But once you understand what each section represents, you‘ll be able to zero in on the key information you need. Let‘s break it down:

The system summary area

The top few lines of the display make up the system summary area, providing a quick overview of system-wide resource usage:

top - 10:32:07 up 2 days, 19:19,  1 user,  load average: 0.75, 0.39, 0.46
Tasks: 271 total,   1 running, 270 sleeping,   0 stopped,   0 zombie
%Cpu(s):  1.9 us,  0.3 sy,  0.0 ni, 97.5 id,  0.2 wa,  0.0 hi,  0.2 si,  0.0 st
MiB Mem :  15691.1 total,   9541.0 free,   2246.0 used,   3904.1 buff/cache
MiB Swap:   2048.0 total,   2048.0 free,      0.0 used.  12641.0 avail Mem

Let‘s decipher each line:

  • The first line shows the current time, system uptime, number of logged in users, and the 1, 5, and 15 minute load averages. Load average indicates the demand on the system‘s resources – the number of processes waiting to run averaged over the given interval.

  • The second line displays a summary of the total number of processes, categorized by state – running, sleeping, stopped, and zombie.

  • The third line breaks down CPU utilization into percentages spent in user mode (us), system mode (sy), low-priority user mode (ni), idle (id), waiting for I/O (wa), servicing hardware interrupts (hi), servicing software interrupts (si), and stolen time (st) in virtualized environments.

  • The fourth and fifth lines provide an overview of physical memory (RAM) and swap space usage respectively. This shows the total amount of each resource, how much is currently used and free, and the available memory (factoring in buffers and cache).

The process list

Below the summary area is the meat of top‘s display – the process list. Each row represents an individual process, with columns providing key data points about that process. By default, processes are sorted by CPU usage, with the hungriest ones at the top.

Some of the most important columns include:

  • PID – The process ID, a unique identifier assigned by the system
  • USER – The username of the process owner
  • PR – The priority of the process, with lower numbers indicating higher priority
  • NI – The nice value of the process, which also affects its scheduling priority
  • VIRT, RES, SHR – The amount of virtual memory, physical memory, and shared memory used by the process
  • S – The process state (e.g. Running, Sleeping, Zombie)
  • %CPU – The share of CPU time used by the process since top last refreshed
  • %MEM – The share of physical memory used by the process
  • TIME+ – The total CPU time used by the process
  • COMMAND – The command that launched the process

Understanding these data points gives you a clear picture of each process‘s resource consumption and status. Armed with this knowledge, you can quickly identify processes that may be misbehaving and diagnose performance issues. But top lets you go further by providing ways to directly manage processes as well.

Interacting with processes in top

In addition to displaying process information, top provides an interactive interface to manage processes in real-time. Some of the most powerful commands let you change the priority (or "nice value") of running processes, kill runaway processes, and even launch new ones.

Adjusting process priority with the r key

Every process on a Linux system has a priority that determines how it‘s scheduled by the kernel. Higher priority processes are allocated more CPU time than lower priority ones. The NI column in top‘s process list shows the "nice value" of each process, which influences its priority.

Nice values range from -20 (highest priority) to 19 (lowest priority), with a default of 0. While top is running, you can change the nice value of a process by pressing the r key. Top will prompt you to enter the PID of the process to renice, then prompt for the new nice value.

For example, to change the nice value of process 1234 to 10, you would:

  1. Press r
  2. Enter 1234 at the PID to renice prompt
  3. Enter 10 at the new nice value prompt

Keep in mind that only root can assign negative (higher priority) nice values. Generally, you should avoid setting extremely high priorities as it can starve other processes of CPU time. For most cases, adjusting nice values in the range of 0 to 19 is sufficient to influence process scheduling.

Renicing a process can be useful in scenarios like:

  • Giving a resource-intensive task like video encoding lower priority to avoid slowing down the rest of the system
  • Boosting the priority of a time-sensitive process like a real-time audio application
  • Ensuring important system services get adequate CPU time in high-load situations

Killing processes with the k key

Occasionally, you may need to forcibly terminate a misbehaving or hung process. Top makes this easy with the k key. Pressing k will prompt you for the PID of the process to kill. By default, top will send SIGTERM to request the process exit cleanly.

You can also specify an alternate signal, such as SIGKILL to forcibly and immediately terminate the process, by entering 9 at the prompt. Exercise caution with SIGKILL as it doesn‘t allow the process to clean up after itself.

Launching processes with the s key

Top also lets you launch a new process directly from its interface with the s key. Pressing s will open a shell prompt where you can enter any command. This can be handy for quickly firing off a related task without leaving top. Once the command finishes, you‘ll be returned to top‘s display.

Customizing and saving top configurations

Like many Linux tools, top‘s behavior can be extensively customized. You can toggle the display of columns, change the sort order, set the delay between refreshes, and more. To make your customizations persistent across top sessions, press W to save your settings to the .toprc file in your home directory.

Some useful customization commands:

  • f – Add or remove columns from the process list
  • o – Change the sort column
  • d – Change the refresh interval
  • V – Toggle display of forest view showing process tree hierarchy

Consult top‘s help screen with h for the full list of interactive commands and customization options. Creating a custom .toprc tailored to your needs can greatly streamline your top workflow.

Going beyond top

While top is a highly capable process monitor on its own, there are several alternatives that offer additional features and enhanced interfaces:

  • htop – A more user-friendly and feature-rich top replacement with a customizable UI and mouse support
  • atop – An advanced performance monitor that logs and replays system and process activity
  • glances – A cross-platform system monitoring tool with a web-based interface and remote monitoring capabilities

Each of these tools builds on top‘s foundation while adding its own unique features and UI enhancements. If you find yourself pushing the limits of what top can do, one of these alternatives may be worth investigating.

Conclusion

The top command is a Swiss Army knife for real-time process monitoring and management on Linux. With its continuously updating display, rich set of process metrics, and interactive process management features, top is an indispensable tool for system administrators and power users.

By mastering top‘s display, interactive commands, and customization options, you‘ll be well-equipped to troubleshoot performance issues, optimize resource usage, and keep your Linux system running smoothly. And with more advanced top alternatives like htop, atop, and glances at your disposal, you‘ll have even more flexibility to tailor your process monitoring workflow.

So next time you need to dive deep into what‘s running on your Linux box, fire up top and put its process prowess to work! With a little practice, you‘ll be navigating top‘s output and wrangling wayward processes like a pro.