As a Linux administrator, you may need to need to know some basic details of your Linux system, such as the currently running processes, average system load, cpu and memory usage etc., at some point. Thankfully, we have a command line utility called "top" to get such details. The top command is a well-known and most widely used utility to display dynamic real-time information about running processes in Linux and Unix-like operating systems. In this brief tutorial, we are going to see some Top command examples on Linux.
Top Command Tutorial With Examples
Here I have listed some most commonly used top command examples.
Monitor all processes
To start monitoring the running processes, simply run the top command without any options:
As you see in the above screenshot, top command displays the list of processes in multiple columns. Each column displays details such as pid, user, cpu usage, memory usage. Apart from the list of processes, you will also see the brief stats about average system load, number of tasks, cpu usage, memory usage and swap usage on the top.
Here is the explanation of the parameters mentioned above.
- PID - Process id of the task.
- USER - Username of the the task's owner.
- PR - Priority of the task.
- NI - Nice value of the task. If the nice value is negative, the process gets higher priority. If the nice value is positive, the priority is low. Refer this guide to know more about nice.
- VIRT - Total amount of virtual memory used by the task.
- RES - Resident Memory Size, the non-swapped physical memory a task is currently using.
- SHR - Shared Memory Size. The amount of shared memory used by a task.
- S - The status of the process (S=sleep R=running Z=zombie).
- %CPU - CPU usage. The task's share of the elapsed CPU time since the last screen update, expressed as a percentage of total CPU time.
- %MEM - Memory Usage. A task's currently resident share of available physical memory.
- TIME+ - Total CPU time used by the task since it has started, precise to the hundredths of a second.
- COMMAND - Name of the running program.
Display path of processes
If you want to see the absolute path of the running processes, just press 'c'. Now you will see the actual path of the programs under the COMMAND column as shown in the below screenshot.
Monitor processes owned by a specific user
If you run top command without any options, it will list all running processes owned by all users. How about displaying processes owned by a specific user? It is easy! To show the processes owned by a given user, for example sk, simply run:
$ top -u sk
Do not show idle/zombie processes
Instead of viewing all processes, you can simply ignore the idle or zombie processes. The following command will not show any idle or zombie processes:
$ top -i
Monitor processes with PID
If you know the PID of any processes, for example 21180, you can monitor that process using -p flag.
$ top -p 21180
You can specify multiple PIDs with comma-separated values.
Monitor processes with process name
I don't know PID, but know only the process name. How to monitor it? Simple!
$ top -p $(pgrep -d ',' firefox)
Here, firefox is the process name and 'pgrep -d' picks the respective PID from the process name.
Display processes by CPU usage
Sometimes, you might want to display processes sorted by CPU usage. If so, use the following command:
$ top -o %CPU
The processes with higher CPU usage will be displayed on the top. Alternatively, you sort the processes by CPU usage by pressing SHIFT+p.
Display processes by Memory usage
Similarly, to order processes by memory usage, the command would be:
$ top -o %MEM
You can change the priority of a process at any time using the option 'r'. Run the top command and press r and type the PID of a process to change its priority.
Here, 'r' refers renice.
Set update interval
Top program has an option to specify the delay between screen updates. If want to change the delay-time, say 5 seconds, run:
$ top -d 5
The default value is 3.0 seconds.
If you already started the top command, just press 'd' and type delay-time and hit ENTER key.
Set number of iterations (repetition)
By default, top command will keep running until you press q to exit. However, you can set the number of iterations after which top will end. For instance, to exit top command automatically after 5 iterations, run:
$ top -n 5
Kill running processes
To kill a running process, simply press 'k' and type its PID and hit ENTER key.
Highlight running processes
Top command supports few other options as well. For example, press 'z' to switch between mono and color output. It will help you to easily highlight running processes.
Press 'h' to view all available keyboard shortcuts and help section.
To quit top, just press q.
At this stage, you will have a basic understanding of top command. For more details, refer man pages.
$ man top
As you can see, using Top command to monitor the running processes isn't that hard. Top command is easy to learn and use!
- How To Find Out How Long A Process Has Been Running In Linux
- Reptyr – Move A Running Process From One Terminal To Another Without Closing It
- How To Suspend A Process And Resume It Later In Linux
- How To Limit CPU Usage Of A Process In Linux
- How To Kill The Largest Process In An Unresponsive Linux System
- How To Find The Execution Time Of A Command Or Process In Linux