From the manpage w(1)
:
w displays information about the users currently on the
machine, and their processes
To display the users' processes, it goes through all processes running on the machine. Let's try this:
$ strace -o w.trace w | grep whatever
Inside the trace we find lines like these (on a Linux system):
open("/proc/8286/cmdline", O_RDONLY) = 4
read(4, "grep\0whatever\0", 2047) = 14
Which shows w
explicitly going through /proc
and looking at the command lines of all processes (and other things, not shown). It finds the grep
that runs parallel to it and that's what strace
sees it do. The pipe has nothing to do with it, other than starting both processes at the same time. In a way, it is similar to ps | grep
seeing the grep itself.
who
and most other commands don't need the information about the processes, and don't go looking, so you don't see the same when tracing them.
w
which needsurandom
. It's becasePiped commands run concurrently
: https://unix.stackexchange.com/questions/37508/in-what-order-do-piped-commands-run – Arkadiusz Drabczyk Apr 24 '17 at 18:02urandom
access inw
source code. – Rui F Ribeiro Apr 24 '17 at 18:04grep
and yourawk
. It's notw
opening the/dev/urandom
device file. It's similar tops -aux | grep grep
– Kusalananda Apr 24 '17 at 18:04strace
so can't test anything. Others will take it and write something good, I'm sure. – Kusalananda Apr 24 '17 at 18:11strace w 2>&1 | grep unicorns
;-) – Digital Trauma Apr 24 '17 at 21:29