Set up Redmine/Mongrel as Service on CentOS

The original post is here http://www.how-to-linux.com/2008/12/set-up-mongrel-as-a-service-and-star...

Below are my modified steps in order to get it to work.

Step 1. Create a file /etc/mongrel/redmine.conf:
# mkdir /etc/mongrel
# vi /etc/mongrel/redmine.conf

and put the following into the file:

# we have only one instance, listening on port 3000
PORTS="3000"
 
# the Rails environment (see config/database.yml of your application)
ENVIRONMENT="production"
 
# the base name for the log file. Each instance will have a unique log file,
# as LOGFILE.PORT
LOGFILE="/opt/redmine/log/redmine.log"
 
# the basename for the pid file. Each instance will have a unique pid file,
# as PIDFILE.PORT
PIDFILE="/opt/redmine/log/mongrel.pid"
 
# the directory containing your RoR application.
ROOTDOCUMENT="/opt/redmine/"
 
# every other extra parameter goes there.
EXTRAPARAMS="--user apache --group apache"

Step 2. Create a file /etc/sysconfig/mongrel:

# vi /etc/sysconfig/mongrel

And put the following into the file:

#Path of mongrel_rails
MONGREL=/usr/local/bin/mongrel_rails

Step 3. Create a file /etc/init.d/mongrel

And put the following into the file:

#!/bin/bash
#
# Mongrel      Startup script for the Mongrel Server
#
# chkconfig: 345 84 20
# description: Mongrel is a Ruby on Rails application server#
# processname: mongrel
# config: /etc/sysconfig/mongrel
# instance config: /etc/mongrel/*.conf
 
# Source function library.
. /etc/rc.d/init.d/functions
 
if [ -f /etc/sysconfig/mongrel ]; then
    . /etc/sysconfig/mongrel
fi
 
# Path to the mongrel_rails server.
mongrel=${MONGREL-/usr/bin/mongrel_rails}
RETVAL=0
 
# For each /etc/mongrel/*.conf file that describes a Ruby on Rails application
# we instantiate the corresponding server instances (one for each port).
# We also cleare the sessions, pids, cache and sockets subdirs.
# Note that you'll hopefully get a [ OK ] line for each (instance,port) pair.
 
start() {
  RET=0
  for instancefile in `ls /etc/mongrel/*conf`; do
        echo -n $instancefile;
 
        unset PORTS ENVIRONMENT LOGFILE PIDFILE ROOTDOCUMENT EXTRAPARAMS
        . $instancefile
 
        for INSTANCEPORT in $PORTS; do
              INSTANCELOG=$LOGFILE.$INSTANCEPORT
              INSTANCEPID=$PIDFILE.$INSTANCEPORT
              CMDLINE="-e $ENVIRONMENT --port $INSTANCEPORT --log $INSTANCELOG -
-pid $INSTANCEPID $EXTRAPARAMS"
 
              if [ -n "$ROOTDOCUMENT" ]; then
 
                    for SUBDIR in "sessions" "pids" "cache" "sockets" ; do
                          rm -f $ROOTDOCUMENT/tmp/$SUBDIR/* 2> /dev/null
                    done
 
                    (cd $ROOTDOCUMENT; $MONGREL start -d $CMDLINE)
                    RETVAL=$?
 
                    if [ "$RETVAL" -ne "0" ]; then
                            RET=1
                    fi
                    echo
              fi
        done
  done
 
  return $RET
}
 
# When stopping mongrel_rails we wait a delay of 10 seconds
stop() {
    echo -n "Stopping Mongrel"
    killproc -d 10 $mongrel
    RETVAL=$?
 
    # We force remove of PID files (a stopped Mongrel won't do that for us)
    for instancefile in `ls /etc/mongrel/*conf`; do
            unset PORTS ENVIRONMENT LOGFILE PIDFILE ROOTDOCUMENT EXTRAPARAMS
            . $instancefile
 
            for INSTANCEPORT in $PORTS; do
                    INSTANCEPID=$PIDFILE.$INSTANCEPORT
                    rm -f $INSTANCEPID 2> /dev/null
            done
    done
    echo
}
 
# See how we were called.
case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  status)
    status $mongrel
    RETVAL=$?
    ;;
  restart)
    stop
    start
    ;;
  *)
    echo "Usage: $prog {start|stop|restart|condrestart|reload|status|fullstatus|graceful|help|configtest}"
    exit 1
esac
 
exit $RETVAL

The first six lines must appear in the file, otherwise chkconfig will complain about "service mongrel does not support chkconfig".In the line "chkconfig: 345 84 20, 345 is the run level, 84 is the start priority, and 20 is stop priority. The start priority should be greater than mysqld, and smaller than httpd. And the stop priority should be greater than httpd, and smaller than mysqld. This way we start mysqld first, then mongrel, then httpd.

Step 4: Add to chkconfig:

# chkconfig --add mongrel
# chkconfig --level 345 mongrel
# service start mongrel

That's it. Now you can check how it is running.