Using supervisord and rvm to run rubyonrails
Asked Answered
H

1

6

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.

In the project folder there is a .ruby-version and a .ruby-gemset 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:

#!/bin/bash

RAILS_ENV="production" rails server -d

My init script looks like this and works besides restarting and stopping:

#!/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 > /dev/null 2>&1 && ./$START_DAEMON_PROCESS &"
        else
          $CD_TO_APP_DIR > /dev/null 2>&1 && ./$START_DAEMON_PROCESS &
        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}" >&2
        exit 1
        ;;
esac

exit 0

My supervisor config looks like this:

[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

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.

$ 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
Headmistress answered 7/7, 2015 at 13:10 Comment(2)
I believe you need a command like: command=/home/myuser/.rvm/wrappers/ruby-2.1.5@myapp/bundle exec rails server -dMelbourne
Supervisor says not to daemonize your subprocesses so don't use the -d option on rails serverBullpen
A
0

Try to source rvm in your script (this link describes usecases like yours).

You have to load RVM into the shell of your script manually:

    source "$HOME/.rvm/scripts/rvm"

It is only enabled for interactive login shells automatically.

From this point on, you can cd into directories and rvm should do its business.

Asclepius answered 8/7, 2015 at 13:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.