- How to view a specific process in top
- 9 Answers 9
- How to Filter Top Output to Specific Processes
- Basic Top Usage
- How to Filter Specific Processes
- How to Filter User Processes
- How to Sort Processes by %CPU
- How to Change Refresh Interval
- How to Show the Path to a Binary
- How to Save The Output to a File
- Final Say
- About the author
- John Otieno
How to view a specific process in top
Are there any relatively strightforward options with top to track a specific process?
Ideally by identifying the process by a human readable value? e.g. chrome or java . In other words, I want to view all the typical information top provides, but for the results to be filtered to the parameters provided i.e.. ‘chrome’ or ‘java’
@Pandya — also, the process I was looking for only ran for a few seconds (node.js during an integration test) — which meant when I used ps -x | process_name to get the PID, when I ran the process again the PID was different and therefore the original PID wouldn’t identify it.
9 Answers 9
From my other answer here, you could do something like,
top -p `pgrep «java»` gives me the following error in a bash shell top: -p requires argument . top -p pgrep -d ‘,’ «apache2» did work for me, but I didn’t really understand what the command was doing — is it way of feeding in multiple arguments to top?
+1 This is the correct answer. «top | grep Chrome» is rather barbaric, because it greps-away ALL OF THE OUTPUT from top not matching «Chrome,» losing stuff like the header and column labels. Using a subshell with the output from pgrep is a correct application of the unix philosophy.
@loretoparisi that may be because the selector you are using matches multiple processes. See this answer for a command that works with one or more matching processes.
You can simply use grep :
NAME grep, egrep, fgrep, rgrep - print lines matching a pattern SYNOPSIS grep [OPTIONS] PATTERN [FILE. ] grep [OPTIONS] [-e PATTERN | -f FILE] [FILE. ] DESCRIPTION grep searches the named input FILEs (or standard input if no files are named, or if a single hyphen-minus (-) is given as file name) for lines containing a match to the given PATTERN. By default, grep prints the matching lines.
Run following command to get output which you want (ex-chrome):
Here we are using grep with pipelines | so top & grep run parallel ; top output given to grep (as input) and grep chrome filters matching lines chrome until top stopped.
How to Filter Top Output to Specific Processes
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.
Basic Top Usage
To use the top utility in Linux, launch a terminal session and enter the command top.
An example output of the top command is:
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.
How to Filter Specific Processes
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:
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.
How to Filter User Processes
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.
The command displays only the processes owned by the ubuntu user.
How to Sort Processes by %CPU
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.
How to Change Refresh Interval
By default, the top will refresh the output every 3 seconds. To modify this value, press the d key while the top is running.
How to Show the Path to a Binary
To show the path to the command that initiated a specific process, press the c key:
How to Save The Output to a File
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:
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).
Final Say
The top is a good utility for system administrators and anyone who wants to know what processes use system resources. To learn more about top, consider the documentation.
About the author
John Otieno
My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list