DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Mongrel Startup Script

  • submit to reddit
        I couldn't find one that did exactly what I wanted, so I cooked up this ruby script. Be sure to change app_dir and apps near the top to match your environment. On my ubuntu/debian server, this script resides in /etc/init.d. I ran "sudo update-rc.d -f mongrel defaults" to make it run on startup.

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

app_dir = '/var/rails'

apps = {
  'hpy' => 8001,
  '43verses' => 8002

if ['stop', 'restart'].include? ARGV.first
  apps.each do |path, port|
    path = File.join app_dir, path
    puts "Stopping #{path}..."
    `mongrel_rails stop -c #{path} -P log/`

if ['start', 'restart'].include? ARGV.first
  apps.each do |path, port|
    path = File.join app_dir, path
    puts "Starting #{path} on #{port}..."
    `mongrel_rails start -d -p #{port} -e production -c #{path} -P log/`

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


Snippets Manager replied on Sun, 2009/04/05 - 4:40pm

I've written a libray which abstracts the common features of init scripts. You just have to implement a start and stop method. The restart method is generated automatically, but you can set a delay in seconds between calling the stop and the start method. You can get it via "gem install init". Documentation can be found here: The example would look like this: #!/usr/bin/env ruby # # Copyright 2009 Alexander E. Fischer # # This file is part of Init. # # Init is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . require 'init' class Mongrel < Aef::Init BASE_DIR = '/srv/rails' APPS = { 'my_project' => 8000, 'demo_app' => 8001 } stop_start_delay 3 # An implementation of the start method def start APPS.each do |app_name, port| puts "Starting #{app_name} on #{port}..." `mongrel_rails start -d -p #{port} -e production -c #{File.join(BASE_DIR, app_name)} -P log/` end end # An implementation of the stop method def stop APPS.each do |app_name, port| puts "Stopping #{app_name}..." `mongrel_rails stop -c #{File.join(BASE_DIR, app_name)} -P log/` end end end # The parser is only executed if the script is executed as a program, never # when the script is required in a ruby program if __FILE__ == $PROGRAM_NAME Mongrel.parse end