An init script for managing delayed_job

If you run delayed_job as a daemon on a server, you’d probably like a way to start it when the system boots. Furthermore, you’re probably using RVM at this stage, which requires its own finicky management details within a script. This little init script will handle all those details for you and make starting and stopping the delayed_job daemon a straightforward process.

You’ll want to modify paths appropriate for your system. Also, although this init script is tuned for Gentoo, it can easily be adapted for Red Hat and other Linux variants.

#!/sbin/runscript
# Distributed under the terms of the GNU General Public License, v2 or later

GEM_HOME=/home/rails/.rvm/gems/ruby-1.9.2-p180
RVM=/home/rails/.rvm/bin/rvm
PASSENGER=/home/rails/.rvm/gems/ruby-1.8.7-p330/bin/passenger
RUBY_VERSION=1.9.2
RUBY_BIN=/home/rails/.rvm/rubies/ruby-1.9.2-p180/bin/ruby

ADDRESS=127.0.0.1
PORT=3010
ENVIRONMENT=staging

APP="/var/rails/commassure"
USER=rails

SET_UP="cd $APP; $RVM use $RUBY_VERSION; export GEM_HOME=$GEM_HOME; export RAILS_ENV=$ENVIRONMENT"
CMD="$SET_UP; $RUBY_BIN script/delayed_job"

# When you stop/start/restart a given service, only the services that 
# 'need' the given service are stopped or restarted. 
#
# The other depending services (those that 'use' the service but don't 
# 'need' it) are left untouched. 

depend() {
    provide delayedjob
    need postgresql
}

start() {
    ebegin "Starting delayed_job daemon"
    #echo $CMD
    su - $USER -c "$CMD start"
    eend $?
}

stop() {
    ebegin "Stopping delayed_job daemon"
    #echo $CMD
    su - $USER -c "$CMD stop"
    eend $?
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s