Tuesday, March 30, 2010

Init script for Redmine

With my new role at work as an Infrastructure Engineer in our Projects team, I've recently started using Redmine, a flexible project management web application written using Ruby on Rails framework, to keep myself and my projects organised.

Redmine is distributed as source and doesn't include any management scripts other than the Ruby ones used to manage Rails. In order to start and stop it automatically on boot on my Fedora 12 workstation, I'm using the following init script.
#!/bin/bash
#
# chkconfig: - 16 84
# description: Start up Redmine
#
# processname: redmine
# config: /etc/sysconfig/redmine

# source function library
. /etc/rc.d/init.d/functions

# Get network config
. /etc/sysconfig/network

[ "${NETWORKING}" = "no" ] && exit 0

# Defaults
DAEMON_HOME=/usr/local/redmine
DAEMON_ARGS="-e production -d"

# Daemon
NAME=redmine
RUBY=$(which ruby)

APP_PIDFILE=$DAEMON_HOME/tmp/pids/server.pid
DAEMON_PIDFILE=/var/run/$NAME.pid
DAEMON_LOCKFILE=/var/lock/subsys/$NAME

start() {
    echo -n $"Starting ${NAME}: "
        
    cd $DAEMON_HOME
    $RUBY script/server webrick $DAEMON_ARGS

    sleep 2

    status -p $APP_PIDFILE &> /dev/null && echo_success || echo_failure
    RETVAL=$?

    if [ $RETVAL -eq 0 ]; then
        touch $DAEMON_LOCKFILE
        cat $APP_PIDFILE > $DAEMON_PIDFILE
    fi

    echo
}

stop() {
    echo -n $"Shutting down ${NAME}: "
    
    killproc -p $DAEMON_PIDFILE
    RETVAL=$?

    [ $RETVAL -eq 0 ] && /bin/rm -f $DAEMON_LOCKFILE $DAEMON_PIDFILE

    echo
}

case "$1" in
    start)
        start
    ;;
    stop)
        stop
    ;;
    restart)
        stop
        start
    ;;
    status)
        status -p $DAEMON_PIDFILE $NAME
    ;;

    *)
        echo "Usage: $SCRIPTNAME {start|stop|restart|status}" >&2
        exit 3
    ;;
esac
Once you have it in place in /etc/init.d, run the following commands to install it, enable Redmine startup on boot and fire it up.
chmod +x /etc/init.d/redmine
chkconfig --add redmine
chkconfig redmine on
/etc/init.d/redmine start

6 comments:

  1. When I run Redmine with /etc/init.d/redmine start all is OK. But
    sudo service redmine start
    gives an error:
    Starting redmine: /usr/lib/ruby/site_ruby/1.8/rubygems.rb:827:in `report_activate_error': Could not find RubyGem rack (~> 1.0.1) (Gem::LoadError)
    What can be wrong?

    ReplyDelete
  2. Hi, I'm not a Ruby or RAILS expert but I'd suggest installing the 'rack' Ruby Gem - you'll notice that the Redmine install instructions list it as a requirement:

    http://www.redmine.org/wiki/1/RedmineInstall

    Hope that helps.

    ReplyDelete
  3. Hello, thank you! I've rack gem. Your script is working fine when is started from command line. It gives the error when I type: service redmine start. It looks like when runs as service it doesn't have necessary environment variables and cannot find right Ruby gems.

    ReplyDelete
  4. I am able to start it on CentOS 5.2 using this but it is not stopping, not sure why. Any idea?

    ReplyDelete
  5. EDIT: my bad - all is fine now :) ~ THANKS!

    ReplyDelete
  6. I have change a little this script for my thin server instead of webrick.
    Thank for this script.
    ----RAKESH KUMAR

    ReplyDelete