Init script for Sidekiq with Rbenv

Sidekiq is a great queuing system for any rails app, it is easy to setup and fast since it also uses Redis. The most difficult part of integrating with a recent app for me, was getting it to start when the VPS booted.

The people behind sidekiq thankfully provide a nice init script, although my particular setup caused a few problems for it.

  • The script should run as root, however I use a 'deployer' user, under which runs rbenv. Root had no access to bundle exec
  • Since I don't write init scripts often, it took me a while to get it to start after Redis

My workaround to the rbenv problem was to modify the providied Sidekiq init script, to run the actual bundle exec sidekiq command as my deployment user:

START_CMD="$BUNDLE exec $SIDEKIQ -e $APP_ENV -P $PID_FILE"  
CMD="cd ${APP_DIR}; ${START_CMD}"

# Replaced this:
$START_CMD >> $LOG_FILE 2>&1 &

# With this:
su -c "$CMD" - $AS_USER  

I created the file /etc/init.d/sidekiq with the contents of the sidekiq init script, and my code above. This didn't work. Examining the log file, sidekiq was being started before redis could finish initializing. To stop this happening I needed to run:

sudo update-rc.d sidekiq defaults 99  

This sets the index of the sidekiq script to be 99. For me Redis was initing at 20, since 99 is greater than 20, Sidekiq will start after.

You can now manage the sidekiq process with the usual:

service sidekiq start  
service sidekiq status  
service sidekiq stop  

Full modified init script below. I have only tried it on Ubuntu 12.10 although it should also work on other Ubuntus and probably Debian.

Hopefully I can save you the time this took me!