Created
April 19, 2014 16:45
-
-
Save itamarhaber/11089937 to your computer and use it in GitHub Desktop.
Kill idle Redis connections
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import redis | |
import re | |
idle_max = 300 | |
r = redis.Redis(host="localhost", port=6379, password=None) | |
l = r.execute_command("client", "list") | |
pattern = r"addr=(.*?) .*? idle=(\d*)" | |
regex = re.compile(pattern) | |
for match in regex.finditer(l): | |
if int(match.group(2)) > idle_max: | |
r.execute_command("client", "kill", match.group(1)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To use, edit and set idle_max to required value, as well as the connection properties. Once done, execute from the shell by doing:
(and don't forget to 'sudo pip install redis' if you haven't already...)