How to use Top and filter it by a specific process only

Top is often the first utility a competent Linux sysadmin reaches for when looking at what’s happening on a system. It’s very much the text equivalent of Windows Task Manager. The problem is Top can be very busy, with constant changes in the order of things not helping the situation. Fortunately there are some commands one can use to filter Top to a specific process only.

The exact command to do that is:

top -p `pgrep -d "," gzip`

Now, instead of being overwhelmed by things moving up and down, you can focus your attention on just the one process that matters.

This command has interesting behavior. If you run it continuously while the process is happening, it will work. But once the process stops, you get this awkward error:

> top -p `pgrep -d "," gzip`
top: -p requires argument

Don’t get fooled. The process might have run but has now stopped.

If what you’re after is actually monitoring two or more processes, try this command:

top -p $(pgrep -d, "(backup-domain|tar|zip|ssh)" )

If the example, above, the system administrator is watching four processes, backup-domain, tar, zip, and ssh to see the process of a backup job that first uses both tar and zip, and then SSH to transfer to another server.

Don’t forget to press ‘c’ on the keyboard to get more information about the running process.

Now if we can get this command sequence to dynamically update as the processes come and go….please leave us a comment.



Share this article

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to Top