Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limit the output of the TOP command to a specific process name

If you call the top command, you get all the running processes. But how can I limit the output only to a certain process name like "java"?

I've tried this top -l 2 | grep java but in this way you get only snapshots and not a continuously updated list. And top -l 0 | grep java is not really clear.

like image 678
Alex Avatar asked Sep 16 '10 14:09

Alex


People also ask

How do you run a process on top?

To run the top command, type top in the command line and press Enter. The command starts in interactive command mode, showing the active processes and other system information. Customize the view using the available options.

How do you stop a top command in a script?

top command option to quit session You need to just press q (small letter q) to quit or exit from top session. Alternatively, you could simply use the traditional interrupt key ^C (press CTRL+C ) when you are done with top command.

How do I view a specific process in Linux?

Type the ps aux to see all running process in Linux. Alternatively, you can issue the top command or htop command to view running process in Linux.


1 Answers

I prefer the following so I can still use top interactively without having to look up the pids each time I run it:

top -p `pgrep process-name | tr "\\n" "," | sed 's/,$//'` 

Of course if the processes change you'll have to re-run the command.

Explanation:

  • pgrep process-name returns a list of process ids which are separated by newlines
  • tr "\\n" "," translates these newlines into commas, because top wants a comma-separated list of process ids
  • sed is a stream editor, and sed 's/,$//' is used here to remove the trailing comma
like image 172
Rick Byers Avatar answered Sep 28 '22 22:09

Rick Byers