While working on the Linux command line, there may be times when you'd want to check process related details, like which process is consuming CPU or m
While working on the Linux command line, there may be times when you’d want to check process related details, like which process is consuming CPU or memory the most? Well, there exists a command – dubbed top – that lets you do this easily. In this tutorial, we will discuss this tool using some easy to understand examples.
But before we do that, it’s worth mentioning that all examples mentioned in this article have been tested on an Ubuntu 16.04 LTS machine.
Linux top command
The top command displays a list of processes or threads currently being managed by the Linux kernel, along with useful information related to them. Following is the tool’s syntax:
And here’s what the tool’s man page says about it:
The top program provides a dynamic real-time view of a running
system. It can display system summary information as well as a
list of processes or threads currently being managed by the Linux
kernel. The types of system summary information shown and the
types, order and size of information displayed for processes are
all user configurable and that configuration can be made persis?
tent across restarts.
The program provides a limited interactive interface for process
manipulation as well as a much more extensive interface for per?
sonal configuration -- encompassing every aspect of its opera?
Following are some Q&A-styled examples that should give you a good idea on how the top command works.
Q1. How to use top?
Simple, just execute the ‘top’ command sans any option:
Here’s the output it produced on my system:
Note that what you see in the screenshot is just a snapshot of the real-time output (which changes after a fixed interval) that was produced.
Q2. How to understand top command output?
Let’s start with the first line. It contains entries for (in that order), current time, duration since the system is up and running, active user sessions, and average load on the system (for last one minute, five minutes, and fifteen minutes, respectively).
The second line contains information (in that order) about total number processes, number of running processes, number of sleeping processes, number of processes that have stopped, and number of processes in the zombie state.
The third line contains CPU-usage information. Following are the details:
us, user : time running un-niced user processes
sy, system : time running kernel processes
ni, nice : time running niced user processes
id, idle : time spent in the kernel idle handler
wa, IO-wait : time waiting for I/O completion
hi : time spent servicing hardware interrupts
si : time spent servicing software interrupts
st : time stolen from this vm by the hypervisor
The next two lines show information related to RAM and swap memory usage. And finally, comes processes related information, which consists of (in that order) process ID, user who owns the process, priority of the process, nice value of the process, virtual, physical, and shared memory used by the process, process status (Sleeping, Running, or Zombie), CPU and RAM usage, time of activity, and command that initiated the process.
Q3. How to kill a process from within top output?
This is pretty simple – just press ‘k’ and top will ask you for the PID of the process you want to kill. You’ll see a message similar to the following appearing just above the line containing process column headers:
PID to signal/kill [default pid = 3397]
So you see, you can either enter the process ID or by default, top will try killing (by sending SIGTERM) the first process in the list. Once the signal is sent, a message similar to the following is produced.
Send pid 3007 signal [15/sigterm]
Q4. How to change priority of a process?
Like ‘k’ is for killing, you can press ‘r’ in case you want to change the priority of a process (or you want to renice it). Pressing ‘r’ produces a message similar to the following:
PID to renice [default pid = 3397]
So either enter a PID or top will try renicing the priority of the first process in list by default. For example, I passed a PID, and then top asked me for the new nice value.
Renice PID 2665 to value
Just provide a value, and press enter, and you’ll see the nice value for the process in question will get changed.
Q5. How to change refresh rate of top command?
By default, top refreshes the output after every 3 seconds, but you can tweak this by pressing ‘d’. The command will ask you to enter the new delay value:
Change delay from 3.0 to
Just enter the value, and press enter. That’s it.
Q6. How to limit top command output to a user?
By default, top command displays all processes. However, you can limit the output to a particular user by pressing ‘u’.
Which user (blank for all)
Just enter the user name in the line above, and press enter, and you’ll see the output will contain only those processes that are owned by the user whose name you entered.
For example, I entered ‘root’, and here’s the output:
Q7. How to make top display complete command along with arguments?
This can be done by pressing ‘c’. Here’s an example:
So you can see complete command path as well as command line arguments in the output now.
Q8. How to get help with interactive top commands?
To get help related to interactive top commands, press ‘h’. In the output you’ll see available commands, and what they do. For example, here’s what popped up on my system when I pressed ‘h’.
We’ve just scratched the surface here. The top command provides a lot more features. Once you have understood what all we’ve discussed in this article, and are done practicing it, head to the top’s man page to learn more about it.