Categories
Self-organization

How to use the linux top command (and understand its output)

Q . Sometime I type command and accidentally hit the [ENTER] key and immeditly realized that it was wrong command. How do I stop process assuming that process is not going in background? For example
cp /path/* /wrong/path

Stop or terminate Linux command process with CTRL + C

A. To stop process hit CTRL + C, for example, you entered:
$ cp -r /path/* /wrong/path

To stop the command hit CTRL+C and retype the command:
$ cp -r /path/* /correct/path

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 4 comments so far. add one

  • How To Kill Process in Linux or Terminate a Process…
  • Disable ctrl+alt+del on Centos Linux server
  • Ubuntu Linux Disable control-alt-delete…
  • Bash Shell Scripting Disable Control-C [ CTRL+C ] Keys
  • Windows like Ctrl + Alt + Delete on MacOS App To Kill Tasks
  • Ubuntu Linux Stop a Process
  • Linux find out what process are eating all memory…
Category List of Unix and Linux commands
Documentation help • mandb • man • pinfo
Disk space analyzers df • duf • ncdu • pydf
File Management cat • cp • less • mkdir • more • tree
Firewall Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04
Modern utilities bat • exa
Network Utilities NetHogs • dig • host • ip • nmap
OpenVPN CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04
Package Manager apk • apt
Processes Management bg • chroot • cron • disown • fg • glances • gtop • jobs • killall • kill • pidof • pstree • pwdx • time • vtop
Searching ag • grep • whereis • which
Shell builtins compgen • echo • printf
User Information groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w
WireGuard VPN Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04

Comments on this entry are closed.

I read that typing ‘reset’ works, even if you cant see it typing. I just use ctrl+c

./ManualManageDB.shn /user/SHARK/log_files/sms.cfg 131022

how can i stop that command, it is loading data into mysql table.

Is there a way I can stop a command without actually switching focus to the terminal window running the command? More specifically I’m trying to stop a script that switches focus to other window. It’s a script running xdotool commands and sometimes it goes wrong and needs to be stopped quickly.

Try opening a terminal and running “top”
Then press “k” and enter the procedure number.

The top command in Linux lets you monitor currently running processes and the system resources they are using. As a system administrator, it can be the most useful tool in your toolbox, especially if you know how to utilize it. The top utility comes pre-installed with all Linux distros. Through this interactive command, you can customize how you can browse the list of processes, kill one, print the results to a file, and much more.

This article describes various ways in which you can use the top command to keep a check on your system activities as an administrator, much more efficiently.

We have run the commands and procedures mentioned in this article on a Ubuntu 18.04 LTS system. We will be using the Ubuntu command line, the Terminal, in order to run our top command. You can open the Terminal either through the system Dash or the Ctrl+Alt+T shortcut.

The top Command Interface

In order to run the top command, open your Ubuntu command line, the Terminal, and enter the following:

You can open the Terminal either through the system Dash or the Ctrl+Alt+T shortcut.

Here is how the output of the top command looks like:

How to use the linux top command (and understand its output)

The upper part of the output shows statistics about the processes and resource usage. The lower part displays a list of currently running processes. In order to browse through the list, you can use the arrow keys or the page up and page down keys.

If you want to quit the command, you can either hit q or use the Ctrl+c shortcut that will terminate the command.

View top Command Help

The top command help will give you a list of options you can use to customize the output and the functions you can perform while you are running the top command.

In order to view help, simply hit the h key while the top command is running.

How to use the linux top command (and understand its output)

Set Interval for Refreshing the Screen

By default, the top command output is refreshed every 3 seconds. In order to change this interval, hit the d key while the top command is running. You can then enter the new time, in seconds, after which the latest output is refreshed on the screen. Advertisement

How to use the linux top command (and understand its output)

Display Processes for a Specific User

If you want the top command to display processes being run by a specific user, you can use the following syntax:

For example, the following command will display all the processes being run by root:

How to use the linux top command (and understand its output)

Highlight Active Processes in Top output

When you hit the z key while your top command is running, the currently active processes will be shown in color, as follows:

How to use the linux top command (and understand its output)

View the Absolute Path of Processes

If you want to view the absolute path of the running processes, hit the c key while the top command is running. In the following output, you can see that the command now displays the path of the running processes in the Command column:

How to use the linux top command (and understand its output)

Kill a Running Process with Top Command

A very powerful feature of the top command is that you can kill running processes through it. This is especially helpful when a process becomes unresponsive and you want to get rid of it. Hit the k key while the top command is running. A prompt will ask you about the PID you want to kill. Enter the required process ID by viewing it from the list and then hit enter. The process and the corresponding application will close almost immediately.

How to use the linux top command (and understand its output)

Change Priority of a Process-Renice

Renice means changing the priority of a process. Hit the r key while the top command is running and then enter the PID of a process whose priority you want to change.

How to use the linux top command (and understand its output)

Automatically Exit top Command after a Specific Number of Refreshes

As mentioned above, you can quit the top command manually through q or ctrl+c. In case you want the top command to only run till a specific number of output refreshes, you can use the following command:

If you enter the following command in the Terminal, the top command will stay active till 3 refreshes and it will automatically quit afterward.

Save top Command Results to a Text File

You can save the current state of your system for later use if you save the output of the top command to a text file.

Here is how you can do so:

$ top -n [no-of-intervals] -b > filename.txt

For example, the following command will save the result of one output of the top command to a file named top.txt:

The file will be saved to the current user’s home folder. If you try to open the file through any of your text editors, the contents will look like this:

How to use the linux top command (and understand its output)

I have opened the file through the nano editor by entering the following command:

Now you are well equipped with a tool that will help you in monitoring and thus managing your system resources in an optimal manner.

  • ← How to Enable/Disable Edge Scrolling in Ubuntu 18.04 LTS
  • How to Change DNS Settings in Ubuntu 18.04 LTS →

Karim Buzdar

About the Author: Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. You can reach Karim on LinkedIn

The top command in Linux lets you monitor currently running processes and the system resources they are using. As a system administrator, it can be the most useful tool in your toolbox, especially if you know how to utilize it. The top utility comes pre-installed with all Linux distros. Through this interactive command, you can customize how you can browse the list of processes, kill one, print the results to a file, and much more.

This article describes various ways in which you can use the top command to keep a check on your system activities as an administrator, much more efficiently.

We have run the commands and procedures mentioned in this article on a Ubuntu 18.04 LTS system. We will be using the Ubuntu command line, the Terminal, in order to run our top command. You can open the Terminal either through the system Dash or the Ctrl+Alt+T shortcut.

The top Command Interface

In order to run the top command, open your Ubuntu command line, the Terminal, and enter the following:

You can open the Terminal either through the system Dash or the Ctrl+Alt+T shortcut.

Here is how the output of the top command looks like:

How to use the linux top command (and understand its output)

The upper part of the output shows statistics about the processes and resource usage. The lower part displays a list of currently running processes. In order to browse through the list, you can use the arrow keys or the page up and page down keys.

If you want to quit the command, you can either hit q or use the Ctrl+c shortcut that will terminate the command.

View top Command Help

The top command help will give you a list of options you can use to customize the output and the functions you can perform while you are running the top command.

In order to view help, simply hit the h key while the top command is running.

How to use the linux top command (and understand its output)

Set Interval for Refreshing the Screen

By default, the top command output is refreshed every 3 seconds. In order to change this interval, hit the d key while the top command is running. You can then enter the new time, in seconds, after which the latest output is refreshed on the screen.

How to use the linux top command (and understand its output)

Display Processes for a Specific User

If you want the top command to display processes being run by a specific user, you can use the following syntax:

For example, the following command will display all the processes being run by root:

How to use the linux top command (and understand its output)

Highlight Active Processes in Top output

When you hit the z key while your top command is running, the currently active processes will be shown in color, as follows:

How to use the linux top command (and understand its output)

View the Absolute Path of Processes

If you want to view the absolute path of the running processes, hit the c key while the top command is running. In the following output, you can see that the command now displays the path of the running processes in the Command column:

How to use the linux top command (and understand its output)

Kill a Running Process with Top Command

A very powerful feature of the top command is that you can kill running processes through it. This is especially helpful when a process becomes unresponsive and you want to get rid of it. Hit the k key while the top command is running. A prompt will ask you about the PID you want to kill. Enter the required process ID by viewing it from the list and then hit enter. The process and the corresponding application will close almost immediately.

How to use the linux top command (and understand its output)

Change Priority of a Process-Renice

Renice means changing the priority of a process. Hit the r key while the top command is running and then enter the PID of a process whose priority you want to change.

How to use the linux top command (and understand its output)

Automatically Exit top Command after a Specific Number of Refreshes

As mentioned above, you can quit the top command manually through q or ctrl+c. In case you want the top command to only run till a specific number of output refreshes, you can use the following command:

If you enter the following command in the Terminal, the top command will stay active till 3 refreshes and it will automatically quit afterward.

Save top Command Results to a Text File

You can save the current state of your system for later use if you save the output of the top command to a text file.

Here is how you can do so:

$ top -n [no-of-intervals] -b > filename.txt

For example, the following command will save the result of one output of the top command to a file named top.txt:

The file will be saved to the current user’s home folder. If you try to open the file through any of your text editors, the contents will look like this:

How to use the linux top command (and understand its output)

I have opened the file through the nano editor by entering the following command:

Now you are well equipped with a tool that will help you in monitoring and thus managing your system resources in an optimal manner.

  • ← How to Enable/Disable Edge Scrolling in Ubuntu 18.04 LTS
  • How to Change DNS Settings in Ubuntu 18.04 LTS →

Karim Buzdar

About the Author: Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. You can reach Karim on LinkedIn

top command is used to show the Linux processes. It provides a dynamic real-time view of the running system. Usually, this command shows the summary information of the system and the list of processes or threads which are currently managed by the Linux Kernel.
As soon as you will run this command it will open an interactive command mode where the top half portion will contain the statistics of processes and resource usage. And Lower half contains a list of the currently running processes. Pressing q will simply exit the command mode.

How to use the linux top command (and understand its output)

Here,

  • PID: Shows task’s unique process id.
  • PR: Stands for priority of the task.
  • SHR: Represents the amount of shared memory used by a task.
  • VIRT: Total virtual memory used by the task.
  • USER: User name of owner of task.
  • %CPU: Represents the CPU usage.
  • TIME+: CPU Time, the same as ‘TIME’, but reflecting more granularity through hundredths of a second.
  • SHR: Represents the Shared Memory size (kb) used by a task.
  • NI: Represents a Nice Value of task. A Negative nice value implies higher priority, and positive Nice value means lower priority.
  • %MEM: Shows the Memory usage of task.

Examples:

1) Exit Top Command After Specific repetition: Top output keep refreshing until you press ‘q‘. With below command top command will automatically exit after 20 number of repetition.

2) Display Specific User Process

How to use the linux top command (and understand its output)

3) Highlight Running Process in Top: Press ‘z‘ option in running top command will display running process in color which may help you to identified running process easily

How to use the linux top command (and understand its output)

4) Shows Absolute Path of Processes: Press ‘c‘ option in running top command, it will display absolute path of running pro

How to use the linux top command (and understand its output)

5) Kill running process: You can kill a process after finding PID of process by pressing ‘k‘ option in running top command without exiting from top window as shown below.

6) Sort by CPU Utilisation: Press (Shift+P) to sort processes as per CPU utilization.

7) Shows top command syntax:

How to use the linux top command (and understand its output)

8) Batch Mode : Send output from top to file or any other programs.

9) Secure Mode : Use top in Secure mode.

10) Command Line : The below command starts top with last closed state.

11) Delay time : It tells delay time between screen updates.

Want to improve this question? Update the question so it’s on-topic for Stack Overflow.

Closed 8 years ago .

I’m using an single core small ARM processor running under Debian and have problems understanding the CPU utilisation output of top, see:

The column %CPU is very low over all processes, in this example it is all together 4,4% (all other process below had been on 0%) But the allover CPU on line 3 shows 65%us and 20%sy, so for both a very high value – and by the way, this is how the system feels: very slow 🙁 The system is almost always in this condition: very low CPU for all processes, but high user+system CPU. Can anybody explain why there is such a high inconsistence within the top tool output? And what tool can I use to better find out what causes the high user+system CPU utilization – top seems to be useless here.

update: meanwhile I’ve found this thread here, which discusses a similiar question, but I can’t verify what is written there:

  • The command uptime shows the average CPU utilization per 1/5/15 minutes
  • This is close to what the first line of top outputs as sum of %us+%sy. But this is changing much more, maybe it is an average per 10s?
  • Even if looking longer time on the top output, the sum of %us+%sy is always several times higher than the summary of all %CPU

Following a discussion made HERE about how PHP-FPM consuming memory, I just found a problem in reading the memory in top command. Here is a screenshot of my top just after restarting PHP-FPM . Everything is normal: about 20 PHP-FPM processes, each consuming 5.5MB memory (0.3% of total).

How to use the linux top command (and understand its output)

Here is the aged server right before restart of PHP-FPM (one day after the previous restart). Here, we still have about 25 PHP-FPM with double memory usage (10MB indicating 0.5% of total). Thus, the total memory used should be 600-700 MB. Then, why 1.6GB memory has been used?

How to use the linux top command (and understand its output)

2 Answers 2

Your server is within some kind of virtuozzo/openvz/virtualization-du-jour container. Trying to make sense of memory usage is tilting at windmills.

Linux ate your RAM! But that’s okay, it does it to everyone.

Let’s break it down!

In the Mem: section we have:

  • $n total : the amount of physical RAM in your machine
  • $n used : how much memory is being consumed by Linux, not just the sum of the processes.
  • $n free : How much RAM is not being consumed by Linux. This does not take into account that cached and buffered memory is in essence “free”.
  • $n buffers : buffer space is where blocks of disk I/O having been read or pending a write are stored. A buffer is a RAM representation of a single disk block.

In the Swap: section we have:

  • $n total : Self explanatory. Amount of disk space available to swap pages to.
  • $n used : Self explanatory. How much disk swap space is used.
  • $n free : Herp Derp.
  • $n cache : Closely related to buffers above. It’s actually part of the page cache and itself has no space on physical disk. Don’t worry about the details for this conversation.

The interesting part comes when you run free -m . You’ll see three lines, and all of the numbers will correlate with top. I’ll give my own PC as an example:

The Mem row shows total RAM in megabytes ( $n total in top), how much is used ( $n used in top), how much is free ( $n free in top), how much is shared (ignore that), and now comes the good part! The buffers and cached columns in free -m correlate to, predictably, $n buffers and $n cache . But take a look at the second row that of free -m that starts with -/+ buffers/cache: . The math shows that the used amount is really (total)-((used-buffers)-cached). Free is (total)-(theNewUsed).

What does all this mean? It means that Linux ate your RAM! The short story is that the Linux kernel gobbles up RAM as it is available to use for disk caching. There’s nothing you can do about it unless you feel like trying to compile a custom kernel. Pro Tip: Don’t.

The RAM is really there and free for processes to use at their whim. That’s what’s meant by the -/+ buffers/cache: row in free -m . However, you’re inside non hyper-visor virtualization container which makes things a bit squirrely. You simply can’t take stock of your memory with byte accuracy at this point. However, you’re not seeing any behavior that’s terribly unusual.

Keep Calm and Carry On. Also, get a physical server (unless you like memory statistics that look like Kreskin is your SysAdmin).

Linux top command is highly used by system administrators to display system statistics in real time regarding system uptime and load average, used memory, running tasks, a summary of processes or threads and detailed information about each running process.

However, besides real time viewing of the running system, top command output can be saved to a file, by using the -b flag, which instructs top to operate in batch mode and -n flag to specify the amount of iteration the command should output.

In the below example, we’ll redirect the output of top command to top.txt file in the current working directory. The -n argument will be used to send only one snapshot of the command to the mentioned file.

To read the resulted file, use a command line file reader utility, such as cat command, less or more.

View Output of Top Command

To grab five iteration of top command, execute the command as shown in the below excerpt.

In order to display only the number of running tasks from the resulted file, use the grep filter, as shown in the below command example.

Show Top 5 Running Tasks

To take a snapshot of a specific process in top utility, execute command with the PID (-p) flag. To get the PID of a running process, issue pidof command against the name of the running process.

In this example we’ll monitor the cron process via top command by taking three snapshots of the PID.

Watch Process Usage in Top Command

Using a for iteration loop, we can display a process statistics via its PID, each two seconds, as shown in the below example. The output of the loop can also be redirected to a file. We’ll use the same cron PID as shown in the above example.

Redirect loop output to file.

Find Linux Process Statistics

These are just a few examples on how you can monitor and gather system and process statistics via top command.

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

We are thankful for your never ending support.

Linux processes FAQ: Is there a utility to show Linux processes interactively, like the Unix/Linux ps command, but more like a GUI ro character-based interactive tool?

I was just writing a friend about how to use the Linux ps command, when I thought why bother, what he really needs to see right now is the top command. The Linux top command is an interactive utility that displays a character-based screen of all processes running on the current system. The screen updates itself every few seconds, and you can sort the screen contents by characteristics like CPU Usage or Memory Use.

In this tutorial we’ll take a look at the most common uses of the top command, including showing how to sort the output by CPU and memory use, and change the top command display.

The Unix and Linux top command

When you issue the top command like this:

you’ll see a screen that looks something like this:

How to use the linux top command (and understand its output)

As mentioned, this display will update itself automatically, depending on any settings that you have configured, and the version of the top command you’re using.

Linux top command help

To get help on how to use the top command, just press the letter ? or h when the command is running. Doing so will display the following top command help screen:

How to use the linux top command (and understand its output)

In case I made that image too small, here’s the content of this top help screen in text format:

Sorting the Linux top command display

In my opinion, the most important thing you need to know from this help screen is how to sort the top command display/output. Using the top command that comes with my CentOS Linux system as of September, 2009, you sort the output by first pressing the O key (the uppercase letter ‘o’), then choosing a sort column on the following screen.

Here are two quick examples. First, to sort the top command output by memory use, follow these steps:

  1. Press ‘O’
  2. Press ‘N’
  3. Press [Enter]

Or, to sort the top command display by CPU usage, follow these steps:

  1. Press ‘O’
  2. Press ‘K’
  3. Press [Enter]

Unix/Linux top command sort options

Here’s the text version of the Linux top command sort options screen:

As you can see from that output, you can sort the top command output by many different pieces of process information. The asterisk indicates that the output is currently sorted by the %CPU column.

Linux and Unix top command – closing notes

To exit/quit the top command, just type the letter q .

The top command implementation is pretty different on Mac OS X. The top command itself brings up a screen, but the columns are different by default, and the commands you can issue are different. I’m not going to get into all the details for Mac OS X, but just use the ? to get help.

You can control “summary” lines that are displayed on the top lines of the top command display. Use the following one-letter commands to toggle the display of the lines at the top of the display:

  • l – load average
  • m – memory
  • t – task/cpu stats

top command

Display tasks and system status in Unix/Linux.

top is a basic Unix command which is very useful for observing the current state of your Unix system, by default presenting you the list of top users of your system’s resources – CPU shares and memory.

Basic usage of the top command

By default, you run top without any parameters, and it shows you a full screen (or full window of your terminal) with the current status of your system and a list of processes using most of its CPU:

Output of the top command explained

These are the elements which default top output consists of:

Unix system uptime and average load

This is the line of top which confirms how many hours (or even days!) your system has been up, shows you the number of logged in users, and reports the average system load numbers for the last minute, 5 minutes and 15 minutes.

  • 13:29:09 is the current time
  • 2 days, 7:13 is the uptime
  • 4 users shows how many users currently use your system
  • 0.07 – average load for the last minute
  • 0.02 – average load for the last 5 minutes
  • 0.00 – average load for the last 15 minutes

Unix tasks stats

Here you can see how many tasks are currently running on your system. Tasks here mean processes, and the main listing will show you the task names (in the COMMAND column) and the PIDs.

CPU(s) status

Current CPU state, averaged for the number of CPUs installed in your system, is represented in this line:

Here are the explanations for each parameter:

  • us – User CPU time. The time the CPU has spent running users’ processes with default priorities
  • sy – System CPU time. The time the CPU has spent running the kernel and its processes
  • ni – Nice CPU time. The time the CPU has spent running users’ proccess that have been prioritized up using nice command
  • wa – I/O wait. Amount of time the CPU has been waiting for I/O operations to complete
  • hi – Hardware IRQ. The amount of time the CPU has been servicing hardware interrupts
  • si – Software Interrupts. The amount of time the CPU has been servicing software interrupts
  • st – Steal Time. The amount of CPU ‘stolen’ from this virtual machine by the hypervisor for other tasks (such as running another virtual machine) – a fairly recent addition to the top command, introduced with the increased virtualization focus in modern operating systems

Physical memory usage stats

Memory stats line gives you a summary of how much physical memory you have on your system, and how much of it is currently used or available for the use.

Modern Linux systems are buffering quite a lot for improved performance, which means you rarely get to see all your physical RAM free – the more your system stays up and running, the more of its recently used data ends up being buffered.

In this line, you can see how quite a bit is taken up by the buffers:

Swap usage stats

Swap statistics highlight how actively your system uses the swap space – most of it should not be used on a healthy system, although seeing substantial amount of swap memory cached is quite normal. Bear in mind that these are caches held in physical memory, so in this example these 2.8Gb of cached swap is responsible for most of the 4Gb physical RAM taken up and reported as used in the above stats for memory

List of the tasks (processes) running on your system

This is the main part of the top output, which looks like this (output is abridged):

As you can see from this list, you’re given all the vital information about each process running on your Unix system:

  • PID – process ID
  • USER – username for the owner of each process
  • PR – process priority (RT means a Real Time priority class – used for system processes)
  • NI – priority set by nice utility
  • VIRT – the amount of virtual memory used by a process: code, data and shared libraries plus pages that have been swapped out
  • RES – the resident part of a process – how much of it resides in the physical memory (non-swapped memory)
  • SHR – shows you the size of potentially shared memory segments for a process
  • S – the current state of each process
  • %CPU – percentage of the time shares CPU spends running a particular process
  • %MEM – percentage of the physical memory of your system which is used by each process
  • %TIME+ – total time CPUs spent running each process
  • COMMAND – a command used to initiate each process.

I’ll be sure to revisit and expand this page at some later stage.