Guest User

Untitled

a guest
Feb 21st, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2. #
  3. # Find bloating passengers and kill them gracefully. Run from cron every minute.
  4. #
  5.  
  6. MEM_LIMIT = ARGV[0] || 500
  7.  
  8. module Process
  9. def self.running?(pid)
  10. begin
  11. return Process.getpgid(pid) != -1
  12. rescue Errno::ESRCH
  13. return false
  14. end
  15. end
  16. end
  17.  
  18. `passenger-memory-stats`.each_line do |line|
  19. if line =~ /Rails: /
  20. parts = line.split
  21. pid, private_dirty_rss = parts[0].to_i, parts[4].to_f
  22.  
  23. if private_dirty_rss > MEM_LIMIT.to_i
  24. puts "Found bloater #{pid} with size #{private_dirty_rss.to_s}"
  25. puts "Killing with SIGUSR1 (graceful)..."
  26. Process.kill("SIGUSR1", pid)
  27. puts "Finished kill attempt. Sleeping for 8 seconds..."
  28. sleep 8
  29. if Process.running?(pid)
  30. puts "Process is still running, so killing with extreme predjudice!"
  31. Process.kill("TERM", pid)
  32. end
  33. puts "Done!"
  34. end
  35.  
  36. end
  37. end
Add Comment
Please, Sign In to add comment