Hosting or Servers Logo

How to Use the Linux top Command

The Linux top command is a useful tool for checking the performance of your system and the processes running on it. The top command displays various information such as CPU usage, memory usage, load average, process ID, user, command, and more. You can also use the top command to perform some actions on the processes, such as changing their priority, sending signals, or killing them.

In this article, we will explain how to use the Linux top command and some of its options and features.

How to run the top command

To run the top command, simply type `top` in your terminal and press Enter. You will see a screen similar to this:

Linux top Command

Linux top Command Running in a Terminal Window

The top command output consists of two parts: the header and the body. The header shows the summary of the system information, such as the time, the uptime, the number of users, the load average, the CPU usage, the memory usage, and the swap usage. The body shows the list of processes sorted by CPU usage by default.


You can use the arrow keys or the Page Up and Page Down keys to scroll through the list of processes. You can also use the Home and End keys to jump to the beginning and the end of the list.

Header Bar Meanings

PID: unique process id.

USER: owner of task.

PR: The process’s priority. The lower the number, the higher the priority.

NI: Nice Value of the task.

VIRT: amount of virtual memory used by the task.

RES: amount of physical RAM the process is using, measured in kilobytes.

SHR: Represents the Shared Memory used by a task.

S: Status of the process

%CPU: Represents the share of CPU usage.

%MEM: Shows the share of Memory usage of task.

TIME+: total CPU time in hundredths of a second.

COMMAND: The name of the command that started the process.

Status Letters

The status of the process can be one of the following:

D: Uninterruptible sleep

I: Idle

R: Running

S: Sleeping

T: Stopped by control signal

Z: Zombie


How to customize the top command output

The top command has many options and features that allow you to customize the output and the behavior of the command. Here are some of the most common ones:


  • To change the sorting order of the processes, you can use the Shift + <key> combination, where <key> is the letter corresponding to the column you want to sort by. For example, to sort by memory usage, you can press Shift + M. To sort by process ID, you can press Shift + P. To sort by user, you can press Shift + U. You can also use the < and > keys to move the sorting column left and right.
  • To change the refresh interval of the top command, you can use the d option followed by the number of seconds. For example, to refresh the output every 5 seconds, you can press d 5 and Enter. To restore the default interval of 3 seconds, you can press d 3 and Enter.
  • To filter the processes by user, you can use the u option followed by the username. For example, to show only the processes belonging to the user bob, you can press u bob and Enter. To show all the processes again, you can press u and Enter without any username.
  • To search for a process by name, you can use the L option followed by the search pattern. For example, to search for a process containing the word kworker, you can press L kworker and Enter. To clear the search, you can press L and Enter without any pattern.
  • To show or hide the header, you can press l (lowercase L). To show or hide the load average, you can press t. To show or hide the memory and swap usage, you can press m.
  • To show or hide the columns, you can use the f option to enter the field management mode. In this mode, you can use the arrow keys to move the cursor, the Space key to toggle the visibility of the column, the d and u keys to move the column down and up, and the s key to change the sorting column. To exit the field management mode, you can press Esc or q.
  • To perform an action on a process, you can use the k option followed by the process ID. This will prompt you to enter a signal number to send to the process. The default signal is 15 (SIGTERM), which will terminate the process gracefully. You can also use other signals, such as 9 (SIGKILL), which will kill the process forcefully, or 19 (SIGSTOP), which will pause the process. To resume a paused process, you can use the signal 18 (SIGCONT).
  • To change the priority (or nice value) of a process, you can use the r option followed by the process ID. This will prompt you to enter a new priority value, which can range from -20 (highest priority) to 19 (lowest priority). The default priority is 0. Note that you need to have root privileges to increase the priority of a process.
  • To quit the top command, you can press q.


Conclusion

The top command is a powerful and versatile tool for monitoring and managing the processes on your Linux system.

Some common use cases and scenarios where the top command can help are:

  • Identifying the processes that are consuming the most CPU, memory, or disk resources, and taking appropriate actions to optimize or terminate them.
  • Checking the overall system load and utilization, and detecting any potential bottlenecks or issues that may affect the system performance or stability.
  • Sorting and filtering the processes based on various criteria, such as user, priority, state, or command name, and finding the information that is most relevant or interesting.
  • Changing the priority or nice value of a process, or sending signals to a process, such as kill, stop, or continue, to control its execution or behavior.
  • Updating the display of the top command in real-time, or setting a custom refresh interval, to observe the changes or trends in the system or process activity over time.


To learn even more about the top command and its features, you can refer to the following resources:


  • The top man page, which provides a detailed description of the top command and its options, flags, and interactive commands. You can access the man page by typing `man top` in your terminal.
  • The Linux top tutorial, which is a comprehensive guide on how to use the top command effectively and efficiently. It covers topics such as how to read the output, how to customize the display, how to filter and sort the processes, how to perform various actions on the processes, and how to troubleshoot common issues. You can find the tutorial at https://www.booleanworld.com/guide-linux-top-command/.

You Are Leaving Us

This link is being provided as a convenience and for informational purposes only it is not an endorsement or an approval of any of the products, services or opinions of the corporation or organization or individual.

Hostingorservers.com bears no responsibility for the accuracy, legality or content of the external site or for that of subsequent links.

You Are Leaving Us

This link is being provided as a convenience and for informational purposes only it is not an endorsement or an approval of any of the products, services or opinions of the corporation or organization or individual.

Hostingorservers.com bears no responsibility for the accuracy, legality or content of the external site or for that of subsequent links.