You are not logged in.

#1 2012-09-18 12:38:31

aardwolf
Member
From: Belgium
Registered: 2005-07-23
Posts: 254

Piping to two processes that both print to stdout

I'd like to use the "ps -eal" command and pipe its output both to "head -n 1" and to "grep vlc".

When running:

ps -eal | grep vlc

You see something like:

0 S 123456 1234     1  1  80   0 - 123456 futex_ ?       00:00:05 vlc

But it would of course be nice to also see the header that ps normally prints.

Running ps -eal | head -n 1 gives:

F S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD

But now I'd like to see these two things combined in a single output, so that the output looks like:

F S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD
0 S 123456 1234     1  1  80   0 - 123456 futex_ ?       00:00:05 vlc

I tried looking up how to redirect bash output to two things, and the best I found was the "tee" command, except that "tee" outputs to filenames.

I tried to use >(head -n 1) as argument to tee, but that did not do what I expected.

So the best I could try was this:

ps -eal | tee >(head -n 1) | grep vlc

But it gives empty output (even though vlc is running). I did some more experiments here, and my conclusion is that actually stdout is glitchy in this case, e.g. when doing

ps -eal | tee >(head)

It outputs something that abruptly stops in the middle without newline at the end.

Is there a way to pipe the output of one process A to two processes B and C, and have the output of B and C printed in the console in the order: first everything from B, then everything from C (and without creating any files on disk)?

Thanks!

EDIT: I found a way to do what I want:

ps -eal | head -n 1 ; ps -eal | grep vlc

However, here ps -eal is repeated twice. I'm interested in a general solution to my question where process A is ran only once.

Last edited by aardwolf (2012-09-18 12:40:17)

Offline

#2 2012-09-18 13:05:09

karol
Archivist
Registered: 2009-05-06
Posts: 25,426

Re: Piping to two processes that both print to stdout

Have you tried abusing ps a bit more?

$ ps -C firefox -o rss -o vsize -o cmd
  RSS    VSZ CMD
337964 696704 firefox

Maybe you can transcribe '-eal' in a similar way.

I think awk can do this too:

$ ps -eal | awk '/WCHAN/;/firefox/'
F S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD
0 S  1000   575   571 12  80   0 - 174644 poll_s pts/1   00:14:08 firefox

Offline

#3 2012-09-18 14:38:43

kokoko3k
Member
Registered: 2008-11-14
Posts: 1,471

Re: Piping to two processes that both print to stdout

If the particular case is enough, and you know how the first line has to look like, you may grep both lines at once instead of using awk (maybe it is lighter)

#ps -eal |grep 'F S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD\|firefox'

F S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD
4 S 1000  30464  676  5  80   0 - 156850 poll_s ?       00:02:16 firefox

Last edited by kokoko3k (2012-09-18 14:41:14)

Offline

#4 2012-09-18 14:47:37

skanky
Member
From: WAIS
Registered: 2009-10-23
Posts: 1,838

Re: Piping to two processes that both print to stdout

karol wrote:

Have you tried abusing ps a bit more?

$ ps -C firefox -o rss -o vsize -o cmd
  RSS    VSZ CMD
337964 696704 firefox

Maybe you can transcribe '-eal' in a similar way.

You can, and you also don't need to keep listing the -o's:

$ ps -C firefox -o rss,vsize,cmd
$ ps -C firefox -l
etc...

See "OUTPUT FORMAT CONTROL" in the ps manual.

karol wrote:

I think awk can do this too:

$ ps -eal | awk '/WCHAN/;/firefox/'
F S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD
0 S  1000   575   571 12  80   0 - 174644 poll_s pts/1   00:14:08 firefox

An alternative way of doing it with awk (might be gawk dependent, but I don't *think* so):

$ ps -eal | awk 'NR == 1;/firefox/'

This will always output the first record, which should be the headings one, and not depend on what those headings contain.

EDIT: add ps info.

Last edited by skanky (2012-09-18 15:23:48)


"...one cannot be angry when one looks at a penguin."  - John Ruskin
"Life in general is a bit shit, and so too is the internet. And that's all there is." - scepticisle

Offline

#5 2012-09-18 15:12:22

jakobcreutzfeldt
Member
Registered: 2011-05-12
Posts: 1,033

Re: Piping to two processes that both print to stdout

Or with sed:

$ ps -eal | sed -n '1p;/vlc/p'

Offline

Board footer

Powered by FluxBB