2

I am piping streamer ouput to mpv player which works fine.

streamer -o -  |  mpv - 

But before starting mpv , I want to kill one program and then pipe to mpv.

streamer program takes some time to buffer video and after 10-15 seconds, it sends to mpv. In those 10-15 seconds, I want to show screen_saver and then kill it just when streamer starts sending to mpv.

Below command just kills the screen_saver but does not run mpv.

streamer -o -  | pkill screen_saver ;  mpv - 
3
streamer -o -  | { dd bs=1 count=1 2>/dev/null; pkill screen_saver; cat; } | mpv -

The first byte from streamer is captured by dd and goes through beyond }. dd immediately terminates, pkill runs, then everything else goes through cat. All the stream should get to mpv.

This requires pkill to be silent on its stdout and read nothing from streamer. My pkill is like that. Just in case you may want to make sure: </dev/null pkill screen_saver >/dev/null.


To test the idea with common tools, run:

cat | { dd bs=1 count=1 2>/dev/null; echo "triggered" >&2; cat; } | cat

and type something (the first cat will probably get anything only after you hit Enter). You will see triggered<newline> inserted just after the first byte of your input. Note this string doesn't disturb the stream the last cat gets, because it's printed to stderr; it's here just to show the triggering occurs when you need it.

Keep in mind dd works with bytes, so if the first character you type is multi-byte (e.g. ś in UTF-8) then the output may surprise you (because your console handles and interlaces stdout and stderr of the commands). This only means the triggering occurred mid-character, just after the first byte, exactly as designed.

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.