Can't pipe redis-cli psubscribe into another command?

360 views
Skip to first unread message

Roger Lipscombe

unread,
Jul 21, 2016, 7:19:37 AM7/21/16
to redi...@googlegroups.com
If I run "redis-cli LRANGE mylist 0 -1 | cat", I can see the output.
On the other hand, if I run "redis-cli PSUBSCRIBE pattern | cat",
nothing appears.

Is this a bug? Am I doing something wrong?

Note that I'm only using "cat" as an example. I'm actually trying to
use "| grep '^{' | jq".

Jan-Erik Rediger

unread,
Jul 21, 2016, 7:33:11 AM7/21/16
to redi...@googlegroups.com
redis-cli output is buffered. It works in the case of LRANGE because
once that is finished, it quits and thus flushes output buffers.

Use the famous Linux utility `stdbuf` to make it line-buffered and
achieve what you want:

stdbuf -oL redis-cli psubscribe pattern | grep '^{' | jq ...

You can make it completely unbuffered using `stdbuf -o0` instead.
> --
> You received this message because you are subscribed to the Google Groups "Redis DB" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to redis-db+u...@googlegroups.com.
> To post to this group, send email to redi...@googlegroups.com.
> Visit this group at https://groups.google.com/group/redis-db.
> For more options, visit https://groups.google.com/d/optout.

Roger Lipscombe

unread,
Jul 21, 2016, 10:38:00 AM7/21/16
to redi...@googlegroups.com, Jan-Erik Rediger
It turns out that you need to apply the buffering options to grep *as well*:

stdbuf -oL redis-cli --raw psubscribe pattern | stdbuf -oL grep "^{" | jq

https://github.com/stedolan/jq/issues/477 talks about 'cut', but it's
the same problem.
Reply all
Reply to author
Forward
0 new messages