4

I was reading this question on rpi and noticed an interesting line that got me thinking:

mkfifo tcp.stream
nc -l -p 1234 > tcp.stream | omxplayer --live tcp.stream

Notice the io redirection. STDOUT is redirected twice! My concern is that omxplayer not having STDIN associated with anything usable might be a little hard to control, but I think this does have the advantage of killing nc with SIGPIPE when omxplayer exits. Is this a good idea? Is this something worth recommending in some odd cases?

6

The pipe will immediately get closed on nc's end. When omxplayer dies nc will receive SIGPIPE on writing to the fifo, not to the pipe.

It might be better to just run nc in the background so that you could keep control over omxplayer via stdin.

mkfifo tcp.stream
nc -l -p 1234 > tcp.stream &
omxplayer --live tcp.stream

However, using | instead of & actually makes sense in the view of shell's job control.

Using |:

$ mkfifo fifo
$ nc -l -p 1234 >fifo | cat fifo

nc and cat belong to the same process group (PGID 9177).

$ ps f -o pid,ppid,pgid,command
  PID  PPID  PGID COMMAND
 9095  1681  9095 bash
 9179  9095  9179  \_ ps f -o pid,ppid,pgid,command
 1691  1681  1691 bash
 9177  1691  9177  \_ nc -l -p 1234
 9178  1691  9177  \_ cat fifo

Both of them receive SIGINT then quit when user types Ctrl+C.

Using &:

$ mkfifo fifo
$ nc -l -p 1234 >fifo & cat fifo
[1] 9183

nc and cat belong to different process groups (PGID 9183 and 9184).

$ ps f -o pid,ppid,pgid,command
  PID  PPID  PGID COMMAND
 9095  1681  9095 bash
 9185  9095  9185  \_ ps f -o pid,ppid,pgid,command
 1691  1681  1691 bash
 9183  1691  9183  \_ nc -l -p 1234
 9184  1691  9184  \_ cat fifo

In this case, only foreground process (cat) receives SIGINT when user types Ctrl+C. If nc doesn't yet get connected, it remains running in the background.

^C
$ jobs
[1]+  Running                 nc -l -p 1234 > fifo &
$ kill %
[1]+  Terminated              nc -l -p 1234 > fifo
  • I was hoping someone would mention this. And it isn't just CTRL+C - the backgrounded processes would keep running even on a shell exit unless job-controlled were specifically enabled w/ set -m for a scripted shell. Run in the pipeline like that they should all be quit at the same time - or suspended together, even - if the parent shell quits or is suspended. – mikeserv Jul 5 '15 at 7:49
  • sh -c 'nc -l -p 1234 > tcp.stream & omxplayer --live tcp.stream' would have the same effect of running nc and omxplayer in the same process group. – Gilles Jul 5 '15 at 12:33
  • I think this latter thing^ needs -m. – mikeserv Jul 5 '15 at 12:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.