Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read stdout of a redis-cli subscribe and run bash command on message

I would like a bash script that reads the stdout of a redis-cli subscribe and does something when a message is received, along with the message value.

The subscribe can be passed in as an arg to redis-cli like so

redis-cli --raw subscribe foo

but then I need to pipe that stdout to something so I can parse the messages. I thought about using a while read line; do loop but a single redis message takes up 3 lines.

update

My endgame is to announce a new redis master by setting a key on etcd. This should be achievable by subscribing to the switch-master channel on a redis sentinel. [1] The value of that message should be something along the lines of <master name> <oldip> <oldport> <newip> <newport> and I would like to set that newip as a value on etcd.

The command I would like to run when that message is received is etcdctl set /redis/master $NEWIP

[1] http://redis.io/topics/sentinel

like image 804
Nick Avatar asked Feb 01 '26 15:02

Nick


1 Answers

because redis-cli subscribe output is currently buffered (https://github.com/antirez/redis/issues/2074) this isn't currently possible.

like image 93
danblack Avatar answered Feb 03 '26 04:02

danblack