How can I prevent 'grep' from showing up in ps results?

  • When I search for some process that doesn't exist, e.g.

    $ ps aux | grep fnord                          
    wayne    15745  0.0  0.0  13580   928 pts/6    S+   03:58   0:00 grep fnord
    

    Obviously I don't care about grep - that makes as much sense as searching for the ps process!

    How can I prevent grep from showing up in the results?

    If you need only the PID of a process, you can replace `ps aux |grep` with `pgrep` (or `pgrep -f` ).

    Same question on Serverfault and Superuser.

    is that output legit? it seems that there IS no program named fnord running...

    @acolyte, that's precisely it - but because it's piping the output into grep, grep is running (waiting for the output of `ps aux`, I expect). So the question is how to prevent `grep fnord` from showing up as a running process because obviously I'm not interested in *that* one.

    no no no no, what i meant is, is that all the output you get? because if so, then there IS no program named 'fnord' running, just the call to grep. if, however, there is a line that you omitted (understandably), which contains the program itself, it's a different problem altogether.

    @acolyte it's not a surprise that you cannot see the "fnord" line. You are not supposed to be able to see it. If you have some 2 or 3 minutes to spare, search for it.

  • Turns out there's a solution found in keychain.

    $ ps aux | grep "[f]nord"
    

    By putting the brackets around the letter and quotes around the string you search for the regex, which says, "Find the character 'f' followed by 'nord'."

    But since you put the brackets in the pattern 'f' is now followed by ']', so grep won't show up in the results list. Neato!

    but, the line itself will be readong " grep fnord (possibly even grep [f]nord"), won't it?

    @acolyte: The output from ps will include a line ending with `grep [f]nord`. However, that line will not make in through the grep filter, because the string `[f]nord` does not match the regular expression `[f]nord`.

    @LarsH interesting, thanks! i would have suggested `ps aux | grep fnord | grep -v grep` ...which seems to have already been suggested by a few people...lol

    If you use `bash`, you don't need to escape or quote the `[]`'s. In `zsh` you only need to escape the first one: `\[c]onky == "[c]onky"`.

    @progo Yes, you do need to quote the `[` even in bash. Try it with a file called `fnord` in the current directory.

    Grepping the output of `ps` may be an old trick, but it's unreliable. Use `pgrep` if available.

    @Gilles, I think I'm slow but I don't get it, what makes the trick "unreliable"?

    @naxa Imagine you have a program call `fnord` and a program called `safnorde`. Or there's a user called `bfnord`, and you end up killing all his processes. Etc.

    Very clever! Nothing else to say, but good thinking.

    Correct me if I am wrong, but this also should work on any position of the grepped character: ps aux| grep "fn[o]rd".

    Where have you been all my life? Why didn't I google this sooner...

    @AmirMehler Here for at least 5 years ;)

    I still don't understand why this works. Anyone can clarify? I understand regex, but why simple using [f] should not match the grep anymore?

    @lucaswxp when you run `ps aux | grep fnord`, that shows up in the output of `ps`, and `grep fnord` finds it. *However*, when you use `ps aux | grep [f]nord` then `grep [f]nord` doesn't match on `[f]nord` - it would only match `fnord`.

    Sure this answer is the best, but these works just for fun: `ps aux | grep "f[n]ord"`, `ps aux | grep "fn[o]rd"`, `ps aux | grep "fno[r]d"`, `ps aux | grep "fnor[d]"`, `ps aux | grep "[f]\{1\}nord"`, ...

    wow, great easy solution `+1`

License under CC-BY-SA with attribution


Content dated before 6/26/2020 9:53 AM