Danny
Danny

Reputation: 4114

Manage sidekiq with init.d script using RVM

I'm using the init.d script provided (the init.d script from the sidekiq github repo), but I am on an ubuntu system with RVM installed system wide.

I cannot seem to figure out how to cd into my app directory and issue the command without there being some complaining in the log and nothing actually starting.

Question: What should the startup command for sidekiq look like in my init.d script when I am using RVM? My user is named ubuntu. Currently I have this in my init.d script:

START_CMD="$BUNDLE exec $SIDEKIQ" 
# where bundle is /usr/local/rvm/gems/ruby-1.9.3-p385/bin/bundle
# and sidekiq is sidekiq
# I've also tried with the following args: -e $APP_ENV -P $PID_FILE -C $APP_CONFIG/sidekiq.yml -d -L $LOG_FILE"
RETVAL=0


start() {

  status
  if [ $? -eq 1 ]; then

[ `id -u` == '0' ] || (echo "$SIDEKIQ runs as root only .."; exit 5)
[ -d $APP_DIR ] || (echo "$APP_DIR not found!.. Exiting"; exit 6)
cd $APP_DIR
echo "Starting $SIDEKIQ message processor .. "
echo "in dir `pwd`"
su - ubuntu -c "$START_CMD >> $LOG_FILE 2>&1 &"
RETVAL=$?
#Sleeping for 8 seconds for process to be precisely visible in process table - See status ()
sleep 8
[ $RETVAL -eq 0 ] && touch $LOCK_FILE
return $RETVAL
  else
    echo "$SIDEKIQ message processor is already running .. "
  fi


}

My sidekiq.log gives me this error:Could not locate Gemfile. However, I print the working directory and I am most definitely in my app's current directory, according to the echo pwd, at the time this command is executed.

When I take out the su - ubuntu -c [command here], I get this error:

/usr/bin/env: ruby_noexec_wrapper: No such file or directory

My solution is to just start the process manually. When I manually cd into my app directory and issue this command:

bundle exec sidekiq -d -L log/sidekiq.log -P tmp/pids/sidekiq.pid

things go as planned, and then

sudo /etc/init.d/sidekiq status

tells me things are up and running.

Also, sudo /etc/init.d/sidekiq stop and status work as expected.

Upvotes: 2

Views: 4036

Answers (2)

cdyer
cdyer

Reputation: 1259

I wrote a blog post a few months ago on my experience writing an init.d script for Sidekiq, however I was using rbenv rather than RVM.

https://cdyer.co.uk/blog/init-script-for-sidekiq-with-rbenv/

I think you should be able to use something almost identical except for modifying the username and app dir variables.

Upvotes: 3

mpapis
mpapis

Reputation: 53158

use wrappers:

BUNDLER=/usr/local/rvm/wrappers/ruby-1.9.3-p385/bundle

in case the bundler wrapper is not available generate it with:

rvm wrapper ruby-1.9.3-p385 --no-links bundle # OR:
rvm wrapper ruby-1.9.3-p385 --no-links --all

you can use aliases to make it easier:

rvm alias create my_app 1.9.3-p385

and then use it like this:

BUNDLER=/usr/local/rvm/wrappers/my_app/bundle

this way you will not have to change the script when application ruby changes - just update the alias, there is a good description/integration for this in rvm-capistrano => https://github.com/wayneeseguin/rvm-capistrano/#create-application-alias-and-wrappers

Upvotes: 2

Related Questions