Displaying articles with tag

MediaTemple (gs) Automated Mongrel Restart

Posted by Matt White, Fri Dec 07 10:45:00 UTC 2007

I’ve got a couple of small Rails sites hosted on MediaTemple’s (gs) product, and I’m happy to say that it’s been a positive experience. A lot of people complained about the product early on, but MediaTemple has made a lot of reliability improvements, and I’ve had nothing but a good experience so far with it.

However, the one problem is that the base Rails container, which runs Mongrel, is pretty limited on memory, and it seems that the mongrel process will just get killed if it goes over it’s limit. I’ve had this happen a couple of times where the process gets killed without being restarted, and then the site is down until I call “cap restart” or restart it via SSH. This obviously isn’t an acceptable solution, so I hacked together a little Ruby script that will check your site to see if it’s alive, and if not it will restart it. Schedule it via cron, and you’re ready to go!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

#!/usr/bin/ruby
require 'net/http'
require 'uri'

site = "your_site_id"
application = "your_application_name"
friendly_name = "Your Site's Actual Name (used only for notification)"
webpath = "http://www.yourdomainname.com/"
user = "serveradmin%yourdomainname.com"
password = "yourpassword"

url = URI.parse(webpath)
req = Net::HTTP::Head.new(url.path)

res = Net::HTTP.start(url.host, url.port) do |http|
  http.request(req)
end

unless res.is_a? Net::HTTPOK
  puts "Detected Site Down: Restarting #{friendly_name}..."
  `mtr restart #{application} --username #{user} --password #{password}`
end

One More Thing

To actually call the script, you have to initialize your PATH variables, since cron doesn’t run your .bash_profile script before it runs your job. I created a .sh file to call my ruby script that sets the correct paths before calling the script:

1
2
3
4
5
6

#!/bin/sh
export PATH=$PATH:/usr/local/bin/

cd $HOME/data/
./babysitter.rb

I called the sh file babysitter.sh, and the ruby script babysitter.rb, chmod +x on both of them, added babysitter.sh as a 5 minute cron job, and forgot about it. Periodically I’ll get a cron email letting me know that it’s restarted the app, though it doesn’t happen very often.

Hopefully this will help someone!

0 comments | Filed Under: | Tags: