Open-Source Internship opportunity by OpenGenus for programmers. Apply now.
In this article, we have explored Top Command in Linux and showed the various information we can get from this command. You should definitely know this command effectively.
Table of contents:
- Introduction to Top Command
- Display All Running Processes
- Sorting The Processes
- Highlight Running Processes
- Absolute path of Running Processes
- Kill A Running Process
- Check Linux CPU Cores
- Save Top Command Results in File
- Alter Screen Refresh Interval
- Exit Top Command After Specific Repetition
- Show/ Hide columns
- Changing Priority of a Process
- When to use Top Command?
Introduction to Top Command
In UNIX-like systems, the top command is pre-installed, there is no need to install any external package. Top command provides real-time information about the running processes.
Top command tendency will be to show as much information as possible but with few keypresses you can change the format and order of the information displayed according to your needs.
Display All Running Processes
- This command will display all running processes and details like Process ID, CPU usage, memory usage, etc.
- It will show summary dashboard first then process list.
top
The column headings in the process list are as follows:
- PID : Process ID.
- USER: The owner of the process.
- PR : Process priority.
- NI : The nice value of the process.
- VIRT : Amount of virtual memory used by the process.
- RES : Amount of resident memory used by the process.
- SHR : Amount of shared memory used by the process.
- S : Status of the process.
Status of the process can be :
- D = Disk sleep
- I = Idle
- R = Running
- S = Sleeping
- T = Stopped
- t = Tracing stop
- X = Dead
- P = Parked
- Z = Zombie
- %CPU : The share of CPU time used by the process since the last update.
- %MEM : The share of physical memory used.
- TIME+ : Total CPU time used by the task in hundredths of a second.
- COMMAND : The command name or command line (name + options).
After executing top command we are on top display and to exit from here press q.
Sorting The Processes
To sort the processes we have some options, use these key combinations to sort :
- Press P = sort by CPU usage
- Press N = sort the list by ProcessID
- Press T = sort by running time
- Press R = reverse the order
- Press M = Sort by the %MEM column.
- Press T = Sort by the TIME+ column.
Press 'X' to highlight the sorted column in bold.
Display All User-Specific Processes :
Displays all User specific processes information
top -u [username]
Highlight Running Processes
Pressing 'z' while the top command is running will highlight the running processes.
Absolute path of Running Processes
Pressing 'c' in running top command will display the absolute path of the running process.
Kill A Running Process
Pressing 'k' in running top command will kill the selected process.
Check Linux CPU Cores
Pressing '1' will load the information of your CPU cores.
Save Top Command Results in File
To save the running top command results output to a file
top -n 1 -b > top.txt
Alter Screen Refresh Interval
Default screen refresh interval is set to 3.0 seconds, to change it press 'd' while the top command is running.
Exit Top Command After Specific Repetition
This command will help you exit top automatically after 10 repetitions of screen refresh.
top -n 10
Show/ Hide columns
By default top displays only few columns out of many more that it can display. If you want to add or remove a particular column or change the order of columns, then press 'f '.
Changing Priority of a Process
Press'r' key while the top command is running and then enter the PID of a process whose priority you want to change, this is known as renice.
If you dont want to remember all the keyboard shortcuts, then an alternative for top command can be Htop command in Linux/ UNIX system.
When to use Top Command?
Top command is one of the most important commands in UNIX if you are using the system professionally. A developer uses top command is the following cases:
- To see the number of CPU cores utilized by a running process (usually, a benchmark) visually. It will give you an idea like 90% of all 64 cores are being utilized.
- To debug scaling of applications. It can happen that applications run slower on large amount of input. One common cause is that application does not use all available cores as in smaller input.
- As an alternative to htop command.
With this article at OpenGenus, you must have the complete idea of Top Command in Linux / UNIX system.