https://phoenixnap.com/kb/top-command-in-linux#see-process-hierarchy
The top is a Linux command-line utility that allows you to monitor running processes and resource usage in your system. Understanding what is going on in your Linux system in real-time can help you locate resource-intensive processes and take necessary actions.
This guide will discuss using the top command in Linux to find specific information about running processes in your system.
To use the top utility in Linux, launch a terminal session and enter the command top.
The output is divided into two portions:
The upper portion shows the resource usage such as Memory, CPU usage, and disk usage.
The lower portion shows information about the running processes. Such data include the Process ID, user, and process name.
To filter the top output to a specific process, press the O key and enter the entry as COMMAND=name, where the name refers to the process name.
For example, to filter for the system:
COMMAND=systemd
Press ENTER, and the top utility will filter the processes to systemd only.
You can also highlight the specific process while keeping other processes in view. To do this, press the L key and search for a string.
The Top output utility also allows us to filter processes spawned by a specific user. To do this, we use the -u flag followed by the username.
For example,
top -u ubuntu
The command displays only the processes owned by the ubuntu user.
We can also sort the top output by the CPU utilization percentage. To do this, press the SHIFT + P command.
The output is sorted in ascending order.
By default, the top will refresh the output every 3 seconds. To modify this value, press the d key while the top is running.
To show the path to the command that initiated a specific process, press the c key:
We can also save the output of the file by using redirection symbols. For example, to export the top output for 10 minutes, use the command:
top -n 3 -b > top_output.log
The -n specifies the number of iterations top should produce before terminating.
The -b flag starts top in batch mode, which continuously processes the output until the maximum iterations are met (specified in -n flag).