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'

  • 1
    have you tried top | grep chrome? – Pandya Oct 31 '14 at 10:59
  • 1
    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
up vote 12 down vote accepted

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.

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

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

top -p `pgrep "java"`
  • 3
    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 1 '14 at 8:33
  • @Ramesh you need to give the pid list comma separated to work. – Kannan Mohan Nov 1 '14 at 13:33
  • 2
    This is the right answer. – j03m Aug 8 '16 at 13:51
  • 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
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 you don't have any java program running, i.e. the pgrep has no output.

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.

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.

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.

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.