How to prevent "ps" reporting its own process?

Solution 1:

+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.

Solution 2:

ps | grep [d]jango

ps | grep d[j]ango


ps | grep djang[o]

Solution 3:

Use pgrep instead: pgrep -lf django

Solution 4:

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:

$ ps -A -ww | grep [f]oobar

Instead I use this pattern:

$ ps -A -ww | grep [^]]foobar

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 from ps, provides a more custom ps 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.

psgrep ()
    case "$(uname -s)" in
            ps -A -ww -o pid,ppid,nice,pri,pcpu,pmem,etime,user,wchan,stat,command | grep -i -e "^[[:space:]]*PID" -e ${pattern}
            ps -A -ww -o pid,ppid,tid,nice,pri,pcpu,pmem,etime,user,wchan:20,stat,command | grep -i -e "^[[:space:]]*PID" -e ${pattern}
        *)  # other UNIX flavors get a minimalist version.
            ps -A -ww | grep -i -e ${pattern}

Solution 5:

Oh wait, this works:

ps | grep django | grep -v grep