title: Using supervisord and rvm to run rubyonrails
body: <p>I have a RubyOnRails 3 project and I'm using rvm. I want to switch from a sysvinit script to supervisord. The sysvinit script can only start the software in case of an error it it gets killed and restarted by $something. Mostly me.</p>
  
  <p>In the project folder there is a <code>.ruby-version</code> and a <code>.ruby-gemset</code> file so that the correct ruby version and gemset gets loaded automatically. Then the app is startet with a shell script which looks like this:</p>
  
  <pre><code>#!/bin/bash
  
  RAILS_ENV="production" rails server -d
  </code></pre>
  
  <p>My init script looks like this and works besides restarting and stopping:</p>
  
  <pre><code>#!/bin/sh
  ### BEGIN INIT INFO
  # Provides:          myapp
  # Required-Start:    $local_fs $remote_fs $network $syslog
  # Required-Stop:     $local_fs $remote_fs $network $syslog
  # Default-Start:     2 3 4 5
  # Default-Stop:      0 1 6
  # Short-Description: starts myapp
  # Description:       starts the myapp software
  ### END INIT INFO
  
  USER=myuser
  PATH=$PATH
  DAEMON=go.sh
  DAEMON_OPTS=""
  NAME=myapp
  DESC="myapp for $USER"
  PID=/home/$USER/myapp/tmp/pids/server.pid
  
  case "$1" in
    start)
          CD_TO_APP_DIR="cd /home/$USER/myapp"
          START_DAEMON_PROCESS="$DAEMON $DAEMON_OPTS"
  
          echo -n "Starting $DESC: "
          if [ $(whoami) = root ]; then
            su - $USER -c "$CD_TO_APP_DIR &gt; /dev/null 2&gt;&amp;1 &amp;&amp; ./$START_DAEMON_PROCESS &amp;"
          else
            $CD_TO_APP_DIR &gt; /dev/null 2&gt;&amp;1 &amp;&amp; ./$START_DAEMON_PROCESS &amp;
          fi
          echo "$NAME."
          ;;
    stop)
          echo -n "Stopping $DESC: "
          kill -QUIT `cat $PID`
          echo "$NAME."
          ;;
    restart)
          echo -n "Restarting $DESC: "
          kill -USR2 `cat $PID`
          echo "$NAME."
          ;;
    reload)
          echo -n "Reloading $DESC configuration: "
          kill -HUP `cat $PID`
          echo "$NAME."
          ;;
    *)
          echo "Usage: $NAME {start|stop|restart|reload}" &gt;&amp;2
          exit 1
          ;;
  esac
  
  exit 0
  </code></pre>
  
  <p>My supervisor config looks like this:</p>
  
  <pre><code>[program:myapp]
  directory=/home/myuser/myapp/
  command=/home/myuser/.rvm/wrappers/ruby-2.1.5@myapp/rails server -d
  environment=RAILS_ENV="production"
  autostart=true
  autorestart=true
  </code></pre>
  
  <p>Problem is that there is no rails binary in the wrapper. so that the command fails. What is the correct way to do this? I'm out of ideas and would start putting some really ugly bash script together that does the job in a very wrong and bad way but does it. Btw I found rails in the gems folder.</p>
  
  <pre><code>$ ls /home/myuser/.rvm/wrappers/ruby-2.1.5@myapp/
  bundle  bundler  erb  executable-hooks-uninstaller  gem  irb  rake  rdoc  ri  ruby  testrb
  $ which rails
  /home/ffwi/.rvm/gems/ruby-2.1.5@ffwi-extern/bin/rails
  </code></pre>