The top command in Linux: real-time process monitor

Introduction to the top command

The top command is one of the most used tools in Linux systems to observe the behavior of processes in real time. It is run from the terminal and displays a dynamic table that updates every few seconds, allowing the administrator to quickly identify what consumes the most CPU, memory, or other resources. Its interface, although text-based, is intuitive and provides a wealth of information without needing to install additional packages.

How top works

When launching top, the program reads information from the /proc pseudo-filesystem and presents it in columns. The first line shows a system summary: current time, uptime, number of users, and average load. Subsequently, each row corresponds to a process and contains data such as PID, owning user, CPU and memory usage percentages, state, and the command that started it.

Main columns and their meaning

  • PID: process identification number.
  • USER: user executing the process.
  • PR and NI: execution priority and its nice value.
  • VIRT: total virtual memory reserved.
  • RES: actual physical memory in use.
  • SHMem: shared memory.
  • %CPU: percentage of CPU consumed since the last update.
  • %MEM: percentage of RAM used.
  • TIME+: total CPU time the process has consumed.
  • COMMAND: name or path of the executable.

Interactive commands within top

While top is running, various keys can be pressed to modify its behavior without exiting the program. Some of the most useful are:

  • k: allows killing a process by entering its PID and the desired signal.
  • r: renices the priority (nice) of a process.
  • c: toggles between showing only the command name and the full path.
  • f: enters a menu to choose which columns to display and in what order.
  • S: changes cumulative time mode, showing CPU time of child processes.
  • h: shows top’s internal help.
  • q: quits the program.

Most used launch options

Top accepts several flags that modify its output from the start. Among the most common are:

  • -d : sets the delay between updates (default 3.0 s).
  • -n <número>: limits the number of iterations before top automatically terminates.
  • -b: runs top in batch mode, useful for redirecting its output to a file or another command without interactivity.
  • -u : filters processes to show only those belonging to a specific user.
  • -p <PID1,PID2,…>: shows only the processes whose IDs are specified.

Practical tips and alternatives

To obtain a more user-friendly view, many administrators prefer using htop, an enhanced version of top with progress bars, colors, and mouse scrolling capability. However, knowing top is essential because it is present in practically any Linux distribution, even in minimal systems where additional packages cannot be installed. Some tips:

  • Use top in batch mode with the -b option and redirect to a file to create resource consumption logs.
  • Combine top with grep to follow a specific process: top -b -n 1 | grep process_name.
  • Observe the average load line to detect spikes that may indicate bottlenecks.
  • Remember that CPU percentage can exceed 100% on multiprocessor systems, as it refers to a single core.

In summary, top remains an indispensable tool for anyone administering Linux servers or workstations. Its ability to provide an instant view of the system state makes it the first point of reference when a performance issue arises.

This post is also available in ESPAÑOL.

Leave a Reply

Your email address will not be published. Required fields are marked *

Esta obra está bajo una Licencia Creative Commons Atribución 4.0 Internacional para Francesc Roig francesc@vivaldi.net .