139

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'

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

10 Answers 10

91

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

top -p `pgrep "java"`
9
  • 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? Commented Nov 1, 2014 at 8:33
  • 1
    @Ramesh you need to give the pid list comma separated to work. Commented Nov 1, 2014 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. Commented Jan 25, 2017 at 16:36
  • 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. Commented Nov 25, 2018 at 15:28
  • 1
    top: -p requires argument means that there is not found any process with specified name. The pgrep returns empty string.
    – DigiBat
    Commented Apr 17, 2023 at 13:40
56

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.

7
  • 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... Commented Nov 1, 2014 at 16:11
  • 10
    This only works if the process is in top's displayed output. I think @Ramesh's answer should be the accepted one
    – j b
    Commented Aug 11, 2017 at 12:18
  • 1
    @JamieBullock the question deals with top only and OP want to filter process based on top.
    – Pandya
    Commented Aug 11, 2017 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
    Commented Aug 11, 2017 at 13:41
  • 2
    I like unix.stackexchange.com/a/165343/48973 better because it shows the headers.
    – Ryan
    Commented Jul 14, 2018 at 17:12
56
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.

4
  • 1
    This solution works better than using top -p pgrep "java"`` only. thank you. Commented Sep 24, 2018 at 8:26
  • 1
    Prevent the error by checking pgrep's exit code : pids="$(pgrep -d, java)" && top -p "$pids"
    – syme
    Commented Nov 30, 2018 at 16:42
  • This solution does not "update" because pgrep is only evaluated once. See @Kusalananda's answer which uses top filtering. Commented Nov 27, 2019 at 1:14
  • 1
    Incidentally, the top option on the default system top on macOS is -pid (for anyone finding themselves here for this answer).
    – hepcat72
    Commented Jun 20, 2021 at 16:27
47

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.

4
  • 4
    This answer has the benefit of updating if a new process begins after calling top! Commented Nov 27, 2019 at 1:12
  • 2
    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
    Commented Jan 13, 2021 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
    Commented Jun 18, 2023 at 3:04
  • We can press = this will clear all filters.
    – devpa
    Commented Jun 18, 2023 at 3: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.

0
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.

1
  • This ^ because RTFM people! man top | less +/5d
    – cprn
    Commented Oct 26, 2018 at 14:09
6

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.

6

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.

1
  • 1
    This works on Amazon Linux 2 AMI (HVM)
    – luis19mx
    Commented Oct 3, 2021 at 14:54
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.

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
1
  • Wonder why this has been downvoted?
    – not2savvy
    Commented Nov 8, 2023 at 19:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .