Ruby on rails 如何告诉rails应用程序中的unicorn shell脚本使用特定的ruby和gemset

Ruby on rails 如何告诉rails应用程序中的unicorn shell脚本使用特定的ruby和gemset,ruby-on-rails,ruby,shell,ruby-on-rails-4,unicorn,Ruby On Rails,Ruby,Shell,Ruby On Rails 4,Unicorn,如何设置unicorn shell文件以使用ruby-2.1。2@my_gemset这样它就可以像sudo服务独角兽一样运行了。MyRails应用程序配置文件夹包含unicorn.sh文件,该文件包含以下内容 #!/bin/sh ### BEGIN INIT INFO # Provides: unicorn # Required-Start: $remote_fs $syslog # Required-Stop: $remote_fs $syslog # Defa

如何设置unicorn shell文件以使用ruby-2.1。2@my_gemset这样它就可以像sudo服务独角兽一样运行了。MyRails应用程序配置文件夹包含unicorn.sh文件,该文件包含以下内容

#!/bin/sh
### BEGIN INIT INFO
# Provides:          unicorn
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Manage unicorn server
# Description:       Start, stop, restart unicorn server for a specific application.
### END INIT INFO
set -e

# Feel free to change any of the following variables for your app:
TIMEOUT=${TIMEOUT-90}
APP_ROOT=/home/user/Documents/workspace/MyApp
PID=$APP_ROOT/tmp/pids/unicorn.pid
CMD="cd $APP_ROOT;bundle exec bin/unicorn -c $APP_ROOT/config/unicorn.rb -E development"
AS_USER="www-data"
action="$1"
set -u

OLD_BIN="$PID.oldbin"

sig () {
  test -s "$PID" && kill -$1 `cat $PID`
}

oldsig () {
  test -s $OLD_BIN && kill -$1 `cat $OLD_BIN`
}

run () {
  if [ "$(id -un)" = "$AS_USER" ]; then
    eval $1
  else
    su -c "$1" - $AS_USER
  fi
}

case "$1" in
start)
  sig 0 && echo >&2 "Already running" && exit 0
  run "$CMD"
  ;;
stop)
  sig QUIT && exit 0
  echo >&2 "Not running"
  ;;
force-stop)
  sig TERM && exit 0
  echo >&2 "Not running"
  ;;
restart|reload)
  sig HUP && echo reloaded OK && exit 0
  echo >&2 "Couldn't reload, starting '$CMD' instead"
  run "$CMD"
  ;;
upgrade)
  if sig USR2 && sleep 3
  then
    n=$TIMEOUT
    while test -s $OLD_BIN && test $n -ge 0
    do
      printf '.' && sleep 1 && n=$(( $n - 1 ))
    done
    echo

    if test $n -lt 0 && test -s $OLD_BIN
    then
      echo >&2 "$OLD_BIN still exists after $TIMEOUT seconds"
      exit 1
    fi
    exit 0
  fi
  echo >&2 "Couldn't upgrade, starting '$CMD' instead"
  run "$CMD"
  ;;
reopen-logs)
  sig USR1
  ;;
*)
  echo >&2 "Usage: $0 <start|stop|restart|upgrade|force-stop|reopen-logs>"
  exit 1
  ;;
esac
但实际上我使用的是ruby-2.1。2@my_gemset. 如何告诉shell脚本使用rvm使用此gemset

我的bin/unicorn文件包含此内容

#!/usr/bin/env ruby
#
# This file was generated by Bundler.
#
# The application 'unicorn' is installed as part of a gem, and
# this file is here to facilitate running it.
#

require 'pathname'
ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
  Pathname.new(__FILE__).realpath)

require 'rubygems'
require 'bundler/setup'

load Gem.bin_path('unicorn', 'unicorn')
我知道我必须将/usr/bin/env ruby的路径更改为我的ruby,但我不知道如何做到这一点。另外,请告诉我如何设置unicorn shell脚本以使用特定的gemset和特定的ruby。谢谢

我正在远程服务器中使用rvm来管理我的gems。我已经提交了.ruby-gemest和.ruby版本,因此它是统一的。在我的部署脚本中,我添加了下面的代码片段,以确保在正确的文件夹中使用正确的gem集运行该命令。因此,执行:

source ~/.rvm/scripts/rvm; cd #{current_path}; /etc/init.d/unicorn #{command}"

我很确定这可以进一步重构,但我已经有一段时间没有时间更新我的脚本了。但到目前为止,它一直在发挥作用。我正在使用Capistrano 3。

我以前没有这样做过,但我认为这会起作用-

加上这一行-

CMD_TO_USE_GEMSET="cd $APP_ROOT; rvm use ruby-2.1.2@my_gemset"
之后

在你的独角兽里

在运行$CMD后,将其写入unicorn.sh的start和restart | reload部分-

现在运行命令重新启动unicorn-

sudo service unicorn start

如果使用,则unicorn shell脚本中的源命令不可用/bin/sh您可以尝试删除源部件并将cd放入目录中吗?这应该足以让它使用正确的gemset。您可以通过执行rvm gemset列表来检查它是否使用了正确的。这是我上次做的。抱歉-删除了我的评论我尝试了,但我得到了rvm未找到错误确保安装了rvm。您可以通过发出以下命令rvm-v来检查它。实际上,rvm已经安装,但unicorn脚本文件没有拾取rvm命令,我也无法在shell脚本中重新加载rvm。还包括源代码~/.rvm/scripts/rvm;在应用程序根目录下不工作请将其放入~/.profile或~/.bashrc:[[-s$HOME/.rvm/scripts/rvm]]&&&$HOME/.rvm/scripts/rvm,这样您就不必为每个会话手动键入它。是的,如果你想使用rvm,它是必要的。如果这不能解决你的问题,我想这会解决问题。请将rvm完全卸下,重新安装rvm。通过遵循正确的步骤。
CMD="cd $APP_ROOT;bundle exec bin/unicorn -c $APP_ROOT/config/unicorn.rb -E development"
run "$CMD_TO_USE_GEMSET"
sudo service unicorn start