Top command is one of the best tools in Unix like operating system for monitoring system resources dynamically. The top command provides a dynamic real-time view of a running system. It can display system summary information as well as a list of tasks/processes currently being managed by the Linux kernel. Here we will go through how to troubleshoot with top command in linux with few examples.
Generally, we use top command without any arguments, but the magic is mostly when top command is used with various options. Well before taking you to that part, let me explain the various system related features which are shown by top command.
Top Command Attributes
Here we will discuss the attributes in top command. The first line of the output of the top command shows the uptime of your system along with logged in users and load average value.
top - 11:27:21 up 1:39, 1 user, load average: 0.24, 0.15, 0.15
The second line in output of top command shows the total process running, sleeping, stopped and zombie process if any.
Tasks: 269 total, 1 running, 268 sleeping, 0 stopped, 0 zombie
The third line shows the CPU Usage.
%Cpu(s): 0.3 us, 1.0 sy, 0.0 ni, 98.7 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
Let’s have a look on different field here.
|us||user cpu time (or) % CPU time spent in user space|
|sy||system cpu time (or) % CPU time spent in kernel space|
|ni||user nice cpu time (or) % CPU time spent on low priority processes|
|id||idle cpu time (or) % CPU time spent idle|
|wa||io wait cpu time (or) % CPU time spent in wait (on disk)|
|hi||hardware irq (or) % CPU time spent servicing/handling hardware interrupts|
|si||software irq (or) % CPU time spent servicing/handling software interrupts|
|st||steal time – %CPU time in involuntary wait by virtual cpu while hypervisor is servicing another processor (or) %CPU time stolen from a virtual machine.|
The next line shows your memory (RAM and swap) usage and capacity.
MiB Mem : 953.3 total, 113.6 free, 521.9 used, 317.8 buff/cache
MiB Swap: 8582.0 total, 8513.5 free, 68.5 used. 277.4 avail Mem
The below sections shows you the details of the process running along with the below details.
|USER||User name of the task’s owner|
|PR||The priority of the task|
|NI||The nice value of the task. A negative nice value means higher priority, whereas a positive nice value means lower priority. Zero in this field simply means priority will not be adjusted in determining a task’s dispatchability|
|%CPU||The task’s share of the elapsed CPU time since the last screen update, expressed as a percentage of total CPU time.|
|%MEM||A task’s currently used share of available physical memory|
|TIME+||Total CPU time the task has used since it started|
The status of the task which can be one of:
|RES||The non-swapped physical memory a task has used|
|SHR||The amount of shared memory used by a task|
|Command||Display the command line used to start a task or the name of the associated program|
Usage of Top Command in Linux
1. Exit Top Command After Specific repetition
To get only specific repetition of the top command in linux command line, run the following command,
$ top -n 10
In above, top command will repeat itself 10 times then exits automatically.
2. Arrange processes with High to Low CPU Usage
Press “P” or “shift+p” once top command is running to arrange all the tasks/processes with High to Low CPU Usage as shown below.
3. Arrange processes with High to Low Memory Usage
Press “M” or “shift+m” once top command is running to arrange all the tasks/processes with High to Low Memory Usage as shown below.
4. Kill any Process
To kill any process, press “k” on the terminal when top is running. Then provide the process id which you want to kill and press enter to kill that specific task.
5. View all the processes running by a specific user
To view all running process, press “u” on the terminal when top is running. You should get a prompt as shown below
$ top -u ravi
6. Show Absolute Path of Processes
Press ‘c‘ option in running top command, it will display absolute path of running processes,
7. Change the nice value (priority) of any process
Press “r” when top is running on the terminal. You should get a prompt as shown below.
Give the PID whose nice value has to be changed and hit “Enter”.
8. Change delay between terminal refresh
By default, top terminal is set for auto refresh after every 3 seconds but if you want you can change it as per your requirement. Press “d” when top is running. You should get a prompt as shown below
9. No. of task to be displayed
By default this option is set to unlimited that is the reason your terminal is fully covered with list of tasks when you run the top command. Any how you can list the no of tasks to be visible once you run top command. Press “n”when top is running. You should get a prompt as shown below
10. View live individual CPU processor performance
By default top command shows you the average of all the available CPUs in the machine. In case you want to see report of all the individual CPUs press “1” once you are running top command and you will get to see something like below.
11, Send output from top to file
To send output from top command to a file, we use batch mode.
$ top -n 1 -b > top-command-output_dd-mm-yyyy.txt
12.Highlight Running Process in Top
To highlight the running processes, press ‘z‘ option while top command is running.
The highlighted are the running processes.
12.Getting help in Top command
To get help while running top command, press ‘z’. This will show all possible arguments you can use with top command.
Hence, in this guide you learnt different scenario of how to Troubleshoot with Top command in linux with examples.
Read Also : Getting Started with Vi/Vim Editor – Advanced