I'm trying to pipe the output of one command to two different awk
commands. Following this post I am using tee
and process substitution. However, I can't see the output for the substituted process.
nvidia-smi | tee >(awk '/ C / {print $6}') | awk '/ C / {print $3}' | xargs -r ps -o user
This is supposed to show the users and memory usage for all gpu processes. The memory usage and PID are extracted from nvidia-smi
, respectively, by awk '/ C / {print $6}'
and awk '/ C / {print $3}'
with the latter then being piped to ps -o user
. The output contains only the users though.
What I would like is
<memory-of-process1> <name-of-user-running-process1>
<memory-of-process2> <name-of-user-running-process2>
<memory-of-process3> <name-of-user-running-process3>
etc
and what I am getting is
<name-of-user-running-process1>
<name-of-user-running-process2>
<name-of-user-running-process3>
etc
I have tried adding fflush()
or stdbuf -o0
to the first awk command, as suggested here.
nvidia-smi | tee >(awk '/ C / {print $6}' > test) | awk '/ C / {print $3}' | xargs -r ps -o user
, and thencat test
prints the memory usages. – ludog May 01 '20 at 12:08awk
withcat
(no arguments), then the user names are printed twice. Likecat
takes stdout as its input after theps
command has run. – ludog May 01 '20 at 12:37awk
is piped to the secondawk
, because the first one inherits the redirection of standard output set by the pipeline. The firstawk
prints one field per line, which gets filtered out by the secondawk
(that single field won't match " C "). To make it work you'll need something along the lines ofecho 0 1 | { tee >(awk '{print $1}' 1>&3) | awk '{print $2}' | xargs -- ps -h -o user -p; } 3>&1
(look at redirections) but, as Paul_Pedant has pointed out, it won't give you the expected output anyway. – fra-san May 01 '20 at 14:39