$ 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...)
+1 for @jamzed terse answer, however the OP might need some explanation:
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
ps | grep [d]jango
ps | grep d[j]ango
...
ps | grep djang[o]
Use pgrep instead:
pgrep -lf django
My answer is a variation on the typical answer for searching for "foobar" in a
ps
listing. The argument of"-A" "ps"
is more portable than"aux"
, I believe, but this change is irrelevant to the answer. The typical answer looks like this:Instead I use this pattern:
The main advantage is that it's easier to write scripts based on this patterns because you simply concatenate a static string
[^]]
with whatever pattern you are looking for. You don't need to strip off the first letter of the string then insert it between the square braces and then concatenate the that back together again. When scripting in shell it's easier to simply stick[^]]
in front of the pattern you were lookginfor looking for. String slicing in Bash is an ugly thing, so my variation avoids that. This variation says show the lines where the pattern matches WITHOUT a leading right-square-bracket ]. Since the search pattern to exclude a square bracket actually adds the square bracket to the pattern then it will never match itself.So you could write a portable
psgrep
command as follows. Here, I make some allowance for differences between Linux, OS X BSD, and others. This adds the column headers fromps
, provides a more customps
format that suits my needs betters, and displays processes listing extra, extra wide so that none of the command-line arguments are missed. Well, most are not missed. Java being Java, it often does things in the worst possible way, so you some java services will run past the maximum allowed length of arguments that the process table will keep track of. I believe this is 1024 characters. The command-lone length allowed to start a process is much longer, but the kernel process table doesn't bother to keep track of anything over 1K in length. Once the command is started the command-name and argument list isn't needed against, so what gets stored in the process table is just informational.Oh wait, this works:
ps -d | grep django
from man ps:
This only occurs to me when running inside an ssh "script" command or when using remote management tools. I don't like the hackiness of the previous solutions. I'm not sure if what I've come up with is better but here you go:
$$ is replaced by the PID of the ps/pgrep "process" and -v tells grep to not print it.
demo:
the
bash -c
and theecho
are only needed to simulate the mentioned situation and because otherwisepgrep
would not report itself.