Skip to content

Instantly share code, notes, and snippets.

@woahdae
Created February 9, 2010 00:41
Show Gist options
  • Save woahdae/298778 to your computer and use it in GitHub Desktop.
Save woahdae/298778 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'net/smtp'
require 'socket'
class MemoryMonitor
def bloated_processes
# the '$11 $12 $13 $14 $15 $16' thing is an ignorant solution for something like '$11 *'
# 'grep -v packet_worker_runner' ignores backgroundrb
`ps auwx | grep -v packet_worker_runner | awk '{ if ($6 > 500000) print $2 "##" $6 "##" $11,$12,$13,$14,$15,$16}'`
end
def kill_bloated_processes
bloated_processes.each do |p|
pid, size, proctitle = p.split("##")
next if proctitle.include?("COMMAND")
Process.kill(9, pid)
message = <<-MESSAGE
Subject: Runaway process on #{Socket.gethostname}
#{proctitle}
Size: #{size.to_i / 1024}MB
MESSAGE
smtp = Net::SMTP.new('smtp.gmail.com', 587)
smtp.enable_starttls
smtp.start('smtp.gmail.com', 'happyrobot@example.com', 'password', :plain)
smtp.send_message message, 'happyrobot@example.com', 'developers@example.com'
smtp.finish
end
end
end
MemoryMonitor.new.kill_bloated_processes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment