top-command-techlinux

What Is Linux Top Command Used For? A Simple Explanation

  • Post author:
  • Post category:Linux
  • Post comments:0 Comments
  • Post last modified:September 2, 2024
  • Reading time:13 mins read

we’re going to go over the linux top command. For those of you who are familiar with the htop utility, top is somewhat similar but is more of an old-school version. It is a command available in most Linux distributions nowadays, whereas htop is not always installed by default.

Even though htop is very cool, you need to learn top as well because sometimes it might be all you have at your disposal. You’ll know exactly how to read its output to understand the health of your Linux server. Let’s dive in and get started!

Basic Usage of the Top Command

To use the top command, simply type top and press Enter. The screen will display the top command interface, and it will be updated every 3 seconds by default. Each update shows a snapshot of the resource usage on your system. It doesn’t matter if you’re using a server or a workstation; top works the same way. The only differences between systems are the number of CPU cores, the amount of memory, and so on.

If you’re experiencing resource contention or if your system is running slower than usual, the top command is a great place to start. If there is any resource contention, you’ll see it in the output. When you’re finished with top, simply press Q to quit and return to the command line.

Let’s go over the top command to make sure you understand everything displayed. There’s a lot of information, and some of it will be useful regularly while other parts might not be as relevant. I’ll walk you through everything you see to ensure you understand the top command.

The top output is divided into two main sections: statistics at the top and a process table at the bottom. We’ll start with the top portion to understand what all those numbers mean.

First Line – Uptime, User, Load Average

top - 17:12:35 up 15 days,  8:28,  2 users,  load average: 0.02, 0.02, 0.00

The first line shows the system’s uptime, the number of users logged in, and the load average. For instance, if the system has been up for about 5 minutes, it indicates how long it has been running. If you’re using a server, an uptime of 5 or 6 minutes might be a problem, especially if you didn’t plan for a restart. It’s something to check if it’s lower than expected.

We also see the number of users logged in. If there’s more than one user on a system where you expect only yourself, it could be a cause for concern.

The load average indicates the system’s performance trend. The general idea is that the load average reflects the percentage of CPU usage. For example, if you have a single-core CPU and the load average is 1, it means the CPU is at 100% usage. If the CPU has four cores, a load average of 4 means 100% usage. There’s more to load average but for a detailed explanation,

Second Line – Tasks

Tasks: 116 total,   1 running, 115 sleeping,   0 stopped,   0 zombie

The second line provides statistics on tasks running on the system. For instance, if there are 116 tasks in total, with one running, 115 sleeping, and none stopped or zombified, it means that currently, only one task is actively using the CPU.

A stopped task is in the process of shutting down, and if you see a stopped task, it means it’s closing. If you see “zombie” tasks, it’s essential to understand what they are. A zombie process is a child process that has completed execution but is still waiting for its parent process to release it. If the parent process has closed but the child is still waiting, it becomes an orphan and remains in the system, which can be a concern, though typically a few zombie processes are harmless.

Third Line – CPU Statistics

The third line covers CPU statistics.

%Cpu(s):  0.0 us,  0.0 sy,  0.0 ni,100.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
  • us refers to user space, where user-level programs run.
  • sy refers to kernel space, where the operating system’s core functions operate.
  • ni stands for niceness, indicating the priority of a process.
  • id represents idle time, showing how much CPU time is spent idle.
  • wa indicates how long tasks have been waiting for input/output operations.
  • hi and si refer to hardware and software interrupts, respectively.
  • st indicates the time a virtual CPU has been waiting for a physical CPU.

This section provides an overview of how the CPU resources are being utilized.

Fourth Line – Memory Information

MiB Mem :    949.5 total,     64.1 free,    543.8 used,    341.6 buff/cache

The fourth line displays memory information. It shows the total amount of memory, free memory, memory in use, and the amount used by the cache. Linux manages the cache efficiently, so normally, you won’t need to intervene. If the system runs low on memory, cached memory will be made available for processes as needed. Unused memory is wasted memory, so cached memory is beneficial.

Fifth Line – Swap Memory

MiB Swap:      0.0 total,      0.0 free,      0.0 used.    252.9 avail Mem 

The fifth line covers swap memory, which is slower than RAM and is located on your hard drive. Some swap usage is normal, but excessive swap usage may indicate that your RAM is running low. This line shows how much swap is being used.

Lower Section Overview

PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND                                                   
1 root 20 0 171144 15896 9036 S 0.0 1.6 2:44.67 systemd
2 root 20 0 0 0 0 S 0.0 0.0 0:00.19 kthreadd
3 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 rcu_gp
4 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 rcu_par_gp
5 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 slub_flushwq
6 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 netns

The lower section of the top the command displays detailed information about the processes using resources. This includes the process ID, the user running the process, the priority of the task, memory and CPU usage, and more. You can sort processes by CPU or memory usage by pressing P for CPU or M memory.

Stopping a Process

PID to signal/kill [default pid = 863252] 

If a process is consuming too many resources or has become frozen, you can press K to kill it. You’ll be prompted to enter the process ID. Each process has a unique ID, and typing it here will terminate the process. Always try to close processes normally before resorting to this method.

Change Delay Update Time

Change delay from 10.0 to 

By default, top updates every 3 seconds, but you can change this by pressing D. After pressing  D, you can specify a new update interval, such as 1 second for more frequent updates.

Conclusion

The top command is a powerful tool for monitoring system performance and managing resources. By understanding its output and functionalities, you can quickly identify performance bottlenecks and make informed decisions to keep your Linux system running efficiently. Practice using top regularly to become more familiar with its capabilities and ensure your system is always performing at its best.

See Also:-

ubuntu.com

techlinux.in

I’m a DevOps engineer with 8 years of experience in automation and cloud infrastructure. I’m passionate about optimizing software delivery and love sharing practical DevOps tips.Outside of work, I enjoy hiking, cooking, and contributing to open-source projects.Thanks for visiting my blog

Leave a Reply