Sunday, 27 November 2016

single command to kill all the redis processes running on a system

A running redis instance can be stopped by executing the command SHUTDOWN. This command will save the running redis into a backup RDB file and stop the redis server. It also has the option of not saving the RDB file and just stopping the redis server by executing "SHUTDOWN NOSAVE".

src/redis-cli SHUTDOWN NOSAVE



Sometimes in redis, particularly when testing redis cluster, we have a number of redis instances running on a system.



When we need to stop all the instances of the redis cluster, we can execute "SHUTDOWN NOSAVE" on all the instances one by one.

A better way is to have a single for loop which does the job

for port in {7000..7005} do ; src/redis-cli -p $port SHUTDOWN NOSAVE ; done ;



What if SHUTDOWN command is renamed or disabled.

Many times, we prefer to disable or rename the SHUTDOWN command as described here, so that it cannot be used by a rogue client. If that is the case, redis servers will need to be stopped by killing the redis processes. Again, the normal way for this is to find out the processes using ps -ef | grep "redis-server" and then manually passing the process ids to kill command. However this can be done by a single command like the below.

ps -ef | grep 'redis-server' | awk '{ print $2}' | xargs kill -9

The above command will find out all the processes having 'redis-server' and get their Ids and pass them to the kill command.

:)

No comments:

Post a Comment