Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top
$ ps | grep django
28006 ttys004    0:01.12 /usr/bin/python bin/django celeryd --beat
51393 ttys005    0:01.45 /usr/bin/python bin/django celeryd -l INFO
51472 ttys005    0:01.29 /usr/bin/python bin/django celeryd -l INFO
51510 ttys005    0:01.89 /usr/bin/python bin/django celeryd -l INFO
51801 ttys005    0:01.83 /usr/bin/python bin/django celeryd -l INFO
53470 ttys005    0:03.97 /usr/bin/python bin/django celeryd -l INFO
53780 ttys005    0:00.00 grep django

Is there a way to prevent the last process (that is, the grep that was started at the same time as my ps command) being reported?

(I started trying to come up with a regex that would match the literal but not match itself, but that seemed, um, not the right approach...)

share|improve this question
up vote 58 down vote accepted

+1 for @jamzed terse answer, however the OP might need some explanation:

ps | grep "[d]jango"

Using that regex you are launching a process which its ps string will not match itself, since the regexp matches "django" and not "[d]jango". That way you'll exclude the process that has the string "[d]jango" which in this case is grep; The same can be applied to pgrep, egrep, awk, sed, etc... whichever command you used to define the regex.

From man 7 regex

   A bracket expression is a list of characters enclosed in "[]".  It nor‐
   mally matches any single character from the list (but see  below).   If
   the  list  begins  with  '^',  it matches any single character (but see
   below) not from the rest of the list.  If two characters  in  the  list
   are  separated  by '-', this is shorthand for the full range of charac‐
   ters between those two (inclusive) in the collating sequence, for exam‐
   ple,  "[0-9]" in ASCII matches any decimal digit.  It is illegal(!) for
   two ranges to share an endpoint, for example, "a-c-e".  Ranges are very
   collating-sequence-dependent,  and portable programs should avoid rely‐
   ing on them.
share|improve this answer
Cool. I'm actually pretty comfortable with regexs but couldn't immediately think of a way to prevent the regexp matching itself. Enclosing a letter in square brackets makes perfect sense. (Including something like [^!] would also work...) – Steve Bennett Mar 9 '12 at 11:23
That's nice and crafty. – ash Mar 9 '12 at 21:09
For the 'ps' specific case, I use '[ ]' at the front of the process name I am searching for. Then I don't need to parse the process name specially for the regex, but it still matches. – Neromancer Nov 5 '14 at 11:05

ps | grep [d]jango

ps | grep d[j]ango


ps | grep djang[o]

share|improve this answer
Add space if you need to grep one char: ps aux| grep "[Z] " – A.D. Jun 5 '15 at 12:12

Use pgrep instead: pgrep -lf django

share|improve this answer
As usual I forgot to mention the platform (OS X in this case). Presumably pgrep works on various linuxes. – Steve Bennett Mar 9 '12 at 11:30
I don't agree, @ramruma . I came to this thread precisely because pgrepgives me exactly this problem. But I must say I am testing it in CygWin (where ps can not show the full command line of the process). – Sopalajo de Arrierez Apr 4 '14 at 2:28

Oh wait, this works:

ps | grep django | grep -v grep
share|improve this answer
Only if the process command line doesn't legitimately include grep, which you cannot count on in the general case. – Michael Kjörling Mar 9 '12 at 12:41

ps -d | grep django

from man ps:

 -d                  Lists information  about  all  processes
                     except session leaders.
share|improve this answer
still shows grep on mine... – Kevin Jun 23 '13 at 21:03
Yep, that works for me on OS X. – Steve Bennett Jan 29 '14 at 5:54
Doesn't work so well on Linux. – A-B-B Aug 18 '14 at 21:55

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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