Strange grepped process list

1

When I run ps fax|grep grep it shows one process like:

 2618 pts/0    R+     0:00              \_ grep --color=auto grep

That would be OK, but when I run ps fax|grep -v dummy_text grep process isn't on the list!

  PID TTY      STAT   TIME COMMAND
    2 ?        S      0:00 [kthreadd]
    3 ?        S      0:02  \_ [ksoftirqd/0]
    5 ?        S      0:00  \_ [kworker/u:0]
    6 ?        S      0:00  \_ [migration/0]
    7 ?        S<     0:00  \_ [cpuset]
    8 ?        S<     0:00  \_ [khelper]
    9 ?        S<     0:00  \_ [netns]
   10 ?        S      0:00  \_ [sync_supers]
   11 ?        S      0:00  \_ [bdi-default]
   12 ?        S<     0:00  \_ [kintegrityd]
   13 ?        S<     0:00  \_ [kblockd]
   14 ?        S<     0:00  \_ [kacpid]
   15 ?        S<     0:00  \_ [kacpi_notify]
   16 ?        S<     0:00  \_ [kacpi_hotplug]
   17 ?        S<     0:00  \_ [ata_sff]
   18 ?        S      0:00  \_ [khubd]
   19 ?        S<     0:00  \_ [md]
   20 ?        S      0:00  \_ [khungtaskd]
   22 ?        S      0:00  \_ [kswapd0]
   23 ?        SN     0:00  \_ [ksmd]
   24 ?        S      0:00  \_ [fsnotify_mark]
   25 ?        S<     0:00  \_ [aio]
   26 ?        S      0:00  \_ [ecryptfs-kthrea]
   27 ?        S<     0:00  \_ [crypto]
   31 ?        S<     0:00  \_ [kthrotld]
   32 ?        S      0:00  \_ [kworker/0:1]
   33 ?        S      0:00  \_ [scsi_eh_0]
   34 ?        S      0:00  \_ [scsi_eh_1]
   35 ?        S      0:00  \_ [kworker/u:2]
   37 ?        S<     0:00  \_ [kmpathd]
   38 ?        S<     0:00  \_ [kmpath_handlerd]
   39 ?        S<     0:00  \_ [kondemand]
   40 ?        S<     0:00  \_ [kconservative]
   41 ?        S      0:00  \_ [kworker/0:2]
  172 ?        S      0:00  \_ [scsi_eh_2]
  185 ?        S<     0:00  \_ [kdmflush]
  198 ?        S<     0:00  \_ [kdmflush]
  206 ?        S      0:00  \_ [jbd2/dm-0-8]
  207 ?        S<     0:00  \_ [ext4-dio-unwrit]
  447 ?        S<     0:00  \_ [kpsmoused]
 2357 ?        S      0:00  \_ [flush-251:0]
    1 ?        Ss     0:00 /sbin/init
  181 ?        S      0:00 /lib/udev/watershed sh -c /sbin/lvm vgscan; /sbin/lvm vgchange -a y
  182 ?        S      0:00  \_ sh -c /sbin/lvm vgscan; /sbin/lvm vgchange -a y
  184 ?        S<L    0:00      \_ /sbin/lvm vgchange -a y
  258 ?        S      0:00 upstart-udev-bridge --daemon
  266 ?        S<s    0:00 udevd --daemon
  433 ?        S<     0:00  \_ udevd --daemon
  434 ?        S<     0:00  \_ udevd --daemon
  365 ?        Sl     0:00 rsyslogd -c4
  436 ?        Ss     0:00 dhclient3 -e IF_METRIC=100 -pf /var/run/dhclient.eth0.pid -lf /var/lib/dhcp3/dhclient.eth0.leases eth0
  473 ?        Ss     0:00 /usr/sbin/sshd -D
  828 ?        Ss     0:00  \_ sshd: censored_user [priv]
  850 ?        S      0:00  |   \_ sshd: censored_user@notty
  851 ?        Ss     0:00  |       \_ /usr/lib/openssh/sftp-server
 2369 ?        Ss     0:00  \_ sshd: censored_user [priv]
 2395 ?        S      0:00      \_ sshd: censored_user@pts/0
 2396 pts/0    Ss     0:01          \_ -bash
 2622 pts/0    R+     0:00              \_ ps fax
  511 ?        S      0:00 upstart-socket-bridge --daemon
  587 tty4     Ss+    0:00 /sbin/getty -8 38400 tty4
  590 tty5     Ss+    0:00 /sbin/getty -8 38400 tty5
  593 tty2     Ss+    0:00 /sbin/getty -8 38400 tty2
  594 tty3     Ss+    0:00 /sbin/getty -8 38400 tty3
  596 tty6     Ss+    0:00 /sbin/getty -8 38400 tty6
  602 ?        Ss     0:00 cron
  603 ?        Ss     0:00 atd
  826 tty1     Ss+    0:00 /sbin/getty -8 38400 tty1

How is that possible?

user95310

Posted 2011-09-01T12:07:09.280

Reputation:

sshd: censored_user. Paranoia? :) – user1686 – 2011-09-01T12:32:16.430

@grawity: Better safe than sorry :P – None – 2011-09-01T12:39:21.457

Answers

2

The -v option to grep tells it to only display lines not containing given text. The ps output line contains dummy_text, which is skipped. In other words, you simply told grep to ignore itself.

user1686

Posted 2011-09-01T12:07:09.280

Reputation: 283 655

sure, but I told grep not to display lines containing "dummy_text", so where is grep? – None – 2011-09-01T12:11:32.797

2@Matej: The grep line itself contains "dummy_text". (More precisely, "grep -v dummy_text".) – user1686 – 2011-09-01T12:12:35.447

Hm are your sure that's the correct answer? I don't think ps will take into account parameters passed to the process. I think the right answer is that ps is executed before grep and thus grep should not show. Please correct me if I'm wrong! – m0skit0 – 2011-09-01T12:23:48.720

1@m0skit0: As you can see from example #1 in the question, ps does show grep, since they are executed at more or less the same time (grep is already running when ps begins iterating the process list). Also, ps does show arguments when any of f, a, or x are given. – user1686 – 2011-09-01T12:26:33.833

0

It's because grep is grabbing itself, at the same time it is running in that micro-tick. You are instantiating a grep instance looking for itself, and sure enough, he finds himself. Then disappears, as the process is terminated in a flash of the eye. Grep is just getting a snapshot of himself in the mirror so-to-speak, that is all.

As compared to the other (second) query, which is saying I am only looking for this particular query. The '-v' is the same as to select non-matching lines. And of course, the query in grep will contain such lines. As it's in the parameters of the process, therefore, ignored.

Stevo Gibbs

Posted 2011-09-01T12:07:09.280

Reputation: 1