2

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.

tripleee
  • 175,061
  • 34
  • 275
  • 318
Kashyap
  • 83
  • 1
  • 6

1 Answers1

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
  • 3
    Thank 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