17

I'm on Ubuntu 18.04. I'm already aware that I can start htop, and then hit F4 for filter, then type or paste a process name search query (say, gphoto2 for looking for gvfs-gphoto2-volume-monitor.service), and then htop filters the shown processes: if there is no such process, it shows nothing, if it starts later, htop shows it then.

Now, I would like to start htop with filtering already from the command line, say:

htop --filter="gphoto2"

... and get the exact same behavior as if hitting F4 and typing in the terminal UI. This would be useful if integrating the command into, say, a tmux session.

I browsed through man htop, but unfortunately I cannot see such command line/option. Closest I've seen is -p, which as noted in how to show only processes *not* matching a filter? :

Something like

htop -p "$(pgrep -vfd, 'java|python')"

This has the obvious disadvantage of not accounting for processes that start after htop starts running

So, is there a way to set up process name filtering on the command line at startup, such that it takes account for newly killed or spawned processes (as if hitting F4 in htops terminal UI) - for htop, or top, or any other similar program?

1
  • 1
    A very useful feature indeed! Have you managed to find a solution? Commented Jun 26, 2019 at 15:22

4 Answers 4

7

Hmm, it's 2021-03-25, I'm running htop version 3.0.5 under Arch and --filter test, -F test, --filter="test" and -F="test" all work for me just fine. This is included in the man page:

COMMAND-LINE OPTIONS
[...]
       -F --filter=FILTER
              Filter processes by command
4

You can do this in top, not htop that I'm aware of.

You want the -p flag. For example: top -p $(pgrep -d',' -f "geany")

This will launch top with only processes for geany shown.

top filtered to geany processes

2
  • 1) The -p is available in Htop. 2) The question specifically asked that “it takes account for newly killed or spawned processes”, which your answer does not provide.
    – Amir
    Commented Feb 9, 2021 at 6:57
  • Better than nothing, though, since on my legacy system, this command works at least, and not with htop, only with top. If you cannot install a newer version of htop, this is already a good start. Commented Apr 20, 2022 at 19:10
2

I went looking for this option, too, and it doesn't exist. I settled for (using your search-term):

\# ps -aux |grep "[0-9] gphoto2"
0

Your commands do not work in older versions, and you likely have one. Perhaps, you are also trying to make your way through a legacy system like me. The accepted answer already puts it right, only older versions have the problem, proof by example:

$ htop -v
htop 1.0.1 - (C) 2004-2011 Hisham Muhammad
Released under the GNU GPL.

$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 12.04.5 LTS
Release:    12.04
Codename:   precise
$ htop --filter test
htop: unrecognized option '--filter'
$ htop --filter "test"
htop: unrecognized option '--filter'
$ htop -F test
htop: invalid option -- 'F'
$ htop -F "test"
htop: invalid option -- 'F'

And from the other answer+comment:

$ htop -p $(pgrep -d',' -f "test")
htop: invalid option -- 'p'
$ top -p $(pgrep -d',' -f "test")
--works--

You must log in to answer this question.

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