131

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'

  • 6
    have you tried top | grep chrome? – Pandya Oct 31 '14 at 10:59
  • 2
    you can also use ps -x | chrome to get pid (let pid shown 2034) and then top | grep 2034 – Pandya Oct 31 '14 at 11:02
  • top | grep chrome worked perfectly - thanks! – Michael Coleman Oct 31 '14 at 11:05
  • @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. – Michael Coleman Oct 31 '14 at 11:15

10 Answers10

88

From my other answer here, you could do something like,

top -p `pgrep "java"`
Ramesh
  • 39,297
  • 10
    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? – Michael Coleman Nov 01 '14 at 08:33
  • 1
    @Ramesh you need to give the pid list comma separated to work. – Kannan Mohan Nov 01 '14 at 13:33
  • 1
    +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. – John M Naglick Jan 25 '17 at 16:36
  • for some reason it works when using certain running commands, but for other it is not. – loretoparisi Sep 24 '18 at 08:25
  • 3
    @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. – Michael Hays Nov 25 '18 at 15:28
  • This solution fails in a situation where short-term java processes come and go - e.g. batch jobs. This solution only matches against processes running at the time the command is issued. – shonky linux user Apr 18 '19 at 00:44
  • When I run this I get "top: -p requires argument" – Nike Dattani Jun 07 '22 at 22:56
  • 1
    top: -p requires argument means that there is not found any process with specified name. The pgrep returns empty string. – DigiBat Apr 17 '23 at 13:40
  • Getting top: invalid option -- 'p' – not2savvy Sep 12 '23 at 14:30
55

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):

top | grep 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.

Pandya
  • 24,618
  • 3
    thanks, I know other people have their preferences, but I like this answer because it is easy to understand, and therefore its easier to remember in the future too! - I would have upvoted but I dont have enough reputation... – Michael Coleman Nov 01 '14 at 16:11
  • 9
    This only works if the process is in top's displayed output. I think @Ramesh's answer should be the accepted one – j b Aug 11 '17 at 12:18
  • 1
    @JamieBullock the question deals with top only and OP want to filter process based on top. – Pandya Aug 11 '17 at 13:26
  • 1
    @Pandya actually, I withdraw my previous comment as it was based on a mistake in my code. Still I think @Ramesh's answer is better (and also filters process based on top). I can easily break yours e.g. with sleep 10 & top | grep sleep – j b Aug 11 '17 at 13:41
  • 2
    I like https://unix.stackexchange.com/a/165343/48973 better because it shows the headers. – Ryan Jul 14 '18 at 17:12
  • 1
    If I press 1 after top it shows individual core utilization info, but not when I grep a process. Any ideas how to resolve this? – Naveen Jan 02 '19 at 18:26
  • @jb is correct. this technique fails if the process of interest does not fall within the small subset of processes displayed on the terminal, for instance if you have a small terminal or the process sorts very low on the list. – BrandonL Feb 04 '22 at 21:05
54
top -p `pgrep -d "," java`

Explanation:

  1. top -p pid1,pid2: show multiple process information, the pid should be separated by ,
  2. pgrep -d "," java: print the pids of all java program, the pids are separated by a newline by default. use the -d "," to separate it by , as required by the top.

If you see error like top: -p argument missing, it means no java program is running, i.e. the pgrep has no output.

44

In OpenBSD top, just press g and enter the command name you'd like to filter on.

In top on e.g. Ubuntu, press o and enter e.g. COMMAND=chrome to only show entries from the COMMAND column that are equal to chrome.

On Linuxes that uses the same top implementation as Ubuntu, read the FILTERING in a Window section in the top manual.

Kusalananda
  • 333,661
  • 4
    This answer has the benefit of updating if a new process begins after calling top! – linux_pangolin Nov 27 '19 at 01:12
  • 1
    It is indeed a nice answer, but I would add that you should just type "=" (without quotation marks) if you want to get rid of filters and enter a new one. If you just enter a new one without deleting the old one, then both will apply, which may or may not be what you intend. – Andyc Jan 13 '21 at 20:28
  • After filtering a specific process using o key and entering COMMAND=ssh how can we go back and see the full list again? – devpa Jun 18 '23 at 03:04
  • We can press = this will clear all filters. – devpa Jun 18 '23 at 03:09
8

Other good answers have been provided, but I made a script some time ago, which I named ptop, that serves me well:

#!/bin/sh
top -p $(pidof "$@" |sed s#\ #,#g) 2>/dev/null
if [ $? -ne 0 ]; then
  echo No processes with the specified name\(s\) were found
fi

This supports multiple process names to be specified (like ptop bash chrome) and provides a nicer error message in case there is/are no processes with any of the specified names running.

Marcelo
  • 3,561
7

If you want to stay in top and keep all other processes in view for context, you can press L to search for your process:

Locate string chrome

This will highlight any process with chrome in the name, and bring it into view. Use & to go to the next match.

You can press c to switch between showing the process name and the full command.

jonatan
  • 191
5

You can also use a filter in top to isolate specific processes. Press 'O' to bring up the filter prompt. Then type a filter formatted as FIELD=value. For example, to filter all tmux processes, use:

COMMAND=tmux

Use '=' to reset filters. See the section titled 'FILTERING' in the top man page.

5

On a Linux terminal:

Type:

top

Then hit the o key, this will prompt you to add a filter. You can then apply a filter to the "COMMAND" column, for example if you wanted to see the "bash" process you can input as a filter:

COMMAND=bash  

This will show only command bash.
Man top (1) for more information, look for FILTER.

Philippos
  • 13,453
Marcelo Pacheco
  • 209
  • 2
  • 3
3

Once you know the PID of the process you wish to track (through running ps auxw |grep your_process) run top in batch mode:

top -b -n 1 -p 1234 | tail -n 1

Where 1234 is your process PID

This approach is better for tracking. It is safer than using grep on a name, more straightforward and uses less resources.

GAD3R
  • 66,769
0

When your top does not know the -p option (top: invalid option -- 'p'), then you can get a similar result using the batch mode with -b:

top -b | grep java
not2savvy
  • 263