bashshellredisredis-cli

Need a script to fetch the redis latency values over 20 seconds and store the results in a file


I need to periodically update a file that will host the Redis Latency values at certain time daily.

I am using the command redis-cli --latency -h 127.0.0.1 -p 6379. The issue is that this command works only in interactive mode and it cycles through the values. I want to get the latency values over a certain time period and then put the values in my file.

When I execute the following command with the timeout utility, it runs for the specified 20 seconds:

timeout 20 /usr/local/bin/redis-cli --latency -h 127.0.0.1 -p 6379

However, when I try to capture the output in a variable (to pass it int the file) like this:

Latency=$(timeout 20 /usr/local/bin/redis-cli --latency -h 127.0.0.1 -p 6379)
echo $Latency

The command stops immediately after running, rather than running for the intended 20 seconds.

How can I capture the Redis latency output for the full duration of 20 seconds into a variable?


Solution

  • When you try to capture Redis Latency output to a variable or file, by default Redis samples the latency for 1 second.

    To customize the duration of the latency test, you can specify the interval between samples using the -i option like this

    Latency=$(/usr/local/bin/redis-cli --latency -h 127.0.0.1 -p 6379 -i 20)
    

    You can get also get this information using the help command for redis latency. Image from Redis help

    redis-cli --latency --help