Init.d

Amos Wenger edited this page Sep 24, 2013 · 2 revisions

When writing a service file where you plan to use a chruby-selected ruby, there are a few important things to keep in mind:

  1. start-stop-daemon requires a single 'daemon' command
  2. chruby needs to be ran from the deploy user

1 means we cannot trivially throw chruby-exec RUBY -- COMMAND in there, or even chruby RUBY && command. Instead, a simple wrapper script can be created.

Here's a wrapper script example that runs rake with chruby-selected MRI 2.0.0:

#!/bin/bash
source /usr/local/share/chruby/chruby.sh && \
chruby ruby-2.0.0 && \
exec rake "$@"

And in the init.d file, one can do something of the sort:

DEPLOY_USER=foo
ROOT=path/to/application/root
PID_FILE=somewhere/pids/myservice.pid
LOG_FILE=somewhere/log/myservice.log

DAEMON="path/to/rake-wrapper"
DAEMON_ARGS="some:rake:task"

case "$1" in
  start)
  start-stop-daemon --start --quiet --pidfile $PID_FILE --exec $DAEMON -c $DEPLOY_USER \
    --background -m -d $ROOT -- $DAEMON_ARGS > $LOG_FILE 2>&1

# etc.

Note that the -c argument here is important - it instructs start-stop-daemon to run the wrapper as the deploy user.