I am exploring redis to do pub/sub. I wanted to write a script that uses redis-cli to subscribe to a channel and dump whatever is published to a file. What I notice however is that redis-cli subscripe channel > output does not quite work.
Asked
Active
Viewed 1,391 times
2
-
See also https://stackoverflow.com/questions/69691811/saving-filtered-redis-cli-output-to-a-file – tripleee Jan 19 '23 at 13:46
1 Answers
0
This is because there is no automatic flush of stdout when redis-cli displays the messages associated to the subscription. So the last messages before stopping redis-cli do not appear in the output file.
There is no option you can use to enforce a systematic flush, redis-cli.c needs to be patched. In Redis source code, edit src/redis-cli.c, and find the following piece of code. Add the missing fflush line.
if (config.pubsub_mode) {
if (config.output != OUTPUT_RAW)
printf("Reading messages... (press Ctrl-C to quit)\n");
while (1) {
if (cliReadReply(output_raw) != REDIS_OK) exit(1);
// The following line must be added
fflush(stdout);
}
}
Once redis-cli has been compiled again, it should work as expected.

Didier Spezia
- 70,911
- 12
- 189
- 154
-
3Thank you so much ... I just got to know from the IRC chat that I could achieve the same effect by running redis-cli like this "stdbuf -oL ./redis-cli .... " – Kashyap Jul 29 '13 at 13:25
-
I did not think about stdbuf, but it should work: redis-cli does use stdout in a very classical way (without explicit adjustment of the I/O buffers). – Didier Spezia Jul 29 '13 at 17:57