Skip to content

Instantly share code, notes, and snippets.

@itamarhaber
Created April 19, 2014 16:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save itamarhaber/11089063 to your computer and use it in GitHub Desktop.
Save itamarhaber/11089063 to your computer and use it in GitHub Desktop.
Kill idle Redis connections
local l = redis.call('client', 'list')
local k = 0
for r in l:gmatch('[^\n]+') do
for c,i in r:gmatch('addr=(.+:%d+).*idle=(%d+).*') do
if tonumber(i) > tonumber(ARGV[1]) then
redis.call('client', 'kill', c)
k = k + 1
end
end
end
return(k)
@itamarhaber
Copy link
Author

To use, save into a file (e.g. hitman.lua) and run as follows:

redis-cli EVAL "$(cat hitman.lua)" 0 300

In this example, connections that have been idle for more than 300 seconds will be killed. The script returns the number of clients killed.

@itamarhaber
Copy link
Author

Yeah, this won't work anymore because CLIENT KILL isn't allowed from scripts.

@macedd
Copy link

macedd commented Mar 1, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment