Informatika Mihelac
March 27 2007 linux | mongrel | rails

Customized Mongrel Startup Script

This shell script allows to start and stop bunch of Rails application on Mongrel webserver. This is Tim Morgan’s script, while I added ability to customize port, rails environment and directory for every app while respecting default values.

I use it on linux development machine where there is no need for mongrel_cluster and I do not want to make extra configuration.

We can save this script as /etc/init.d/mongrel and start|restart|stop all mongrels with /etc/init.d/mongrel start (or stop|restart). And to configure for startup we can use:

# redhat
sudo /sbin/chkconfig --level 345 mongrel on
#debian/ubuntu
sudo /usr/sbin/update-rc.d -f mongrel defaults

And here is script:

#!/usr/bin/env ruby
#
# mongrel Startup script for Mongrel by Tim Morgan, modified by bmihelac
#
# chkconfig: - 85 15
# description: mongrel manages Mongrel
#

# this would add one mongrels for these two apps on 
# ports 8000 and 8001, mephisto would be started in production
apps = [
  {:app => 'myapp'},
  {:app => 'mephisto', :environment => 'production'},
]

default_port = 8000
default_options = {
  :app_dir => '/home/rails',
  :environment => 'production'
}

if ['stop', 'restart'].include? ARGV.first
  apps.each do |app|
    options = default_options.merge(app)
    path = File.join options[:app_dir], options[:app]
    puts "Stopping #{path}..." 
    `mongrel_rails stop -c #{path} -P log/mongrel.pid`
  end
end

if ['start', 'restart'].include? ARGV.first
  apps.each do |app|
    options = default_options.merge(app)
    path = File.join options[:app_dir], options[:app]
    port = options[:port] || default_port
    puts "Starting #{options[:app]} on #{port}..." 
    `mongrel_rails start -d -p #{port} -e #{options[:environment]} -c #{path} -P log/mongrel.pid`
    default_port = port + 1
  end
end

unless ['start', 'stop', 'restart'].include? ARGV.first
    puts "Usage: mongrel {start|stop|restart}" 
    exit
end

comments feed

6 comments

  1. # On March 31, 2007 at 15:03 PM, Bojan said:
    Thanks Boris, I have updated script.
  2. # On April 11, 2007 at 08:04 AM, chris said:
    Awesome stuff! Thanks.
  3. # On August 29, 2007 at 22:08 PM, Clayton Cottingham said:

    this is the best script ive found so far

    also you might want to add /sbin/chkconfig—level 06 mongrel off

    to kill mongrel

    as well you might want to add in the ability to use ips:

    default_ip = ‘127.0.0.1’ and further on :

    ip = options[:ip] || default_ip puts “Starting #{options[:app]} on #{port}...” `mongrel_rails start -d -p #{port} -a #{ip} -e #{options[:environment]} -c #{path} -P log/mongrel.pid`

  4. # On December 19, 2007 at 18:12 PM, carli said:

    Hi! Today getting a credit card is easy. Almost every credit card company issues credit cards for any credit score. Great? Sure. You can solve your financial problems with the right credit card.

    best deal on discover cards discover miles card 0% apr for life on transferred balances

  5. # On December 19, 2007 at 18:12 PM, carli said:

    Hi! Today getting a credit card is easy. Almost every credit card company issues credit cards for any credit score. Great? Sure. You can solve your financial problems with the right credit card.

    best deal on discover cards discover miles card 0% apr for life on transferred balances

  6. # On December 19, 2007 at 18:12 PM, carli said:

    Hi! Today getting a credit card is easy. Almost every credit card company issues credit cards for any credit score. Great? Sure. You can solve your financial problems with the right credit card.

    best deal on discover cards discover miles card 0% apr for life on transferred balances

Speak your mind:

(Required)

(Required)


(You may use textile in your comments.)

About

I am Bojan Mihelac and this blog is dedicated to share code, thoughts, tools and advices I came up with while working in Informatika Mihelac.

Contact: bmihelac@mihelac.org

RSS feedSubscribe to RSS Feed