Skip to content

Instantly share code, notes, and snippets.

@kazjote
Created January 2, 2015 10:53
Show Gist options
  • Save kazjote/af37908b5cd8737e38f9 to your computer and use it in GitHub Desktop.
Save kazjote/af37908b5cd8737e38f9 to your computer and use it in GitHub Desktop.
Script to kill resque processes running > 1 day
#!/usr/bin/env ruby
# Kills resque workers that are there for longer than 24 hours
processes = `ps ax -o pid,cmd`.split("\n")
processes_by_pid = processes.inject({}) do |hash, line|
pid, cmd = line.split(" resque-1.25.2: ")
hash unless pid && cmd
hash.update(pid.strip => cmd)
end
def in_past?(cmd, regexp)
_, time = regexp.match(cmd).to_a
time && Time.at(time.to_i).utc < Time.now.utc - 24 * 60 * 60
end
processes_to_kill = processes_by_pid.each_with_object([]) do |(pid, cmd), result|
if in_past?(cmd, /Forked \d+ at (\d+)/) || in_past?(cmd, /Processing \w+ since (\d+)/)
result << pid
end
end
`kill -9 #{processes_to_kill.join(' ')}`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment