Observe:
% echo ping |redis-cli Could not connect to Redis at 127.0.0.1:6379: Connection refused Could not connect to Redis at 127.0.0.1:6379: Connection refused % echo $? 0
This makes redis-cli pretty useless for scripting.
Well, just pass the command as an argument then:
[~]% redis-cli ping; echo $?
Could not connect to Redis at 127.0.0.1:6379: Connection refused
1
[~]% redis-server&
[~]% redis-cli ping; echo $?
PONG
0
Thanks, that should do it.
It seems that this behaviour can still be seen in some cases (when trying to flush all keys on slave instance):
$ redis-cli -p 6379 flushall ; echo $?
(error) READONLY You can't write against a read only slave.
0
$redis-cli --version
redis-cli 2.8.19
redis-cli has over 70 different exit points, so there's no clean way to do the right thing everywhere. If some of them aren't working for you, feel free to fix the exit values and open a PR.