ruby-on-rails - unicorn 服务 Upstart 脚本抛出 "-su: bundle: command not found"

标签 ruby-on-rails ubuntu-14.04 unicorn rbenv upstart

我最近在 DigitalOcean 上创建了一个 VPS 来托管一个 rails 应用程序。我按照他们的指南使用我的应用程序设置 Unicorn。 https://www.digitalocean.com/community/tutorials/how-to-deploy-a-rails-app-with-unicorn-and-nginx-on-ubuntu-14-04

运行 sudo service unicorn_appxyz start 时出现问题。给出的错误是 -su: bundle: command not found

我跟踪了 init.d 脚本并将评估的服务器启动命令粘贴到终端中,当在用户 joe 下执行时它工作正常(安装 rbenv 的用户和应用程序的所有者)。评估的命令是

su - joe -c cd /home/joe/appxyz && bundle exec unicorn -c config/unicorn.rb -E production -D

然后我 sudo su - 进入 root 用户并运行 service unicorn_appxyz start 错误当然是一样的。然后我在 root 下运行了评估的命令,它返回了这个错误

The program 'bundle' is currently not installed. You can install it by typing:
apt-get install bundler

脚本似乎没有切换用户?这可能是我启动 VPS 时 unicorn 无法启动的原因。

完整的 unicorn Upstart 脚本在这里:

#!/bin/sh

### BEGIN INIT INFO
# Provides:          unicorn
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts the unicorn app server
# Description:       starts unicorn using start-stop-daemon
### END INIT INFO

set -e

USAGE="Usage: $0 <start|stop|restart|upgrade|rotate|force-stop>"

# app settings
USER="joe"
APP_NAME="appxyz"
APP_ROOT="/home/$USER/$APP_NAME"
ENV="production"

# environment settings
PATH="/home/$USER/.rbenv/shims:/home/$USER/.rbenv/bin:$PATH"
CMD="cd $APP_ROOT && bundle exec unicorn -c config/unicorn.rb -E $ENV -D"
PID="$APP_ROOT/shared/pids/unicorn.pid"
OLD_PID="$PID.oldbin"

# make sure the app exists
cd $APP_ROOT || exit 1

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

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

case $1 in
  start)
    sig 0 && echo >&2 "Already running" && exit 0
    echo "Starting $APP_NAME"
    su - $USER -c "$CMD"
    ;;
  stop)
    echo "Stopping $APP_NAME"
    sig QUIT && exit 0
    echo >&2 "Not running"
    ;;
  force-stop)
    echo "Force stopping $APP_NAME"
    sig TERM && exit 0
    echo >&2 "Not running"
    ;;
  restart|reload|upgrade)
    sig USR2 && echo "reloaded $APP_NAME" && exit 0
    echo >&2 "Couldn't reload, starting '$CMD' instead"
    $CMD
    ;;
  rotate)
    sig USR1 && echo rotated logs OK && exit 0
    echo >&2 "Couldn't rotate logs" && exit 1
    ;;
  *)
    echo >&2 $USAGE
    exit 1
    ;;
esac

更多相关信息

这里是用户 joe 下的 ruby​​、rails 和 bundler 的路径。在根目录下找不到它们。

joe@vps:~$ which ruby
/home/joe/.rbenv/shims/ruby
joe@vps:~$ which rails
/home/joe/.rbenv/shims/rails
joe@vps:~$ which bundle
/home/joe/.rbenv/shims/bundle

在 root 用户下找不到 bundler 是有道理的,但是 upstart 命令应该已经切换到用户 'joe' 来运行 bundle 命令。这是我不明白的部分。

最佳答案

我发现了问题。解释如下,

root 用户在启动时将首先 su - 进入 rails 用户(在本例中为“joe”),然后执行 bundle 以启动 unicorn。 rbenv 是单用户,只有 'joe' 安装了包。捆绑的路径可能存储在我的 .bashrc 文件中。但是 .bashrc 文件不是通过 su 登录调用的 - 并且导致 bundle 未安装错误。

我在 .profile 中包含了与 rbenv 相关的路径。这样,当 root su - 进入 'joe' 时,路径就会被加载。

关于ruby-on-rails - unicorn 服务 Upstart 脚本抛出 "-su: bundle: command not found",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31971806/

相关文章:

ruby-on-rails - 按姓氏组织联系人以按字母顺序排列的列表中显示

java - 托管使用Java和Rails代码的Web应用程序?

ruby-on-rails - 尝试加载 gem 'yard' 时出错

ruby-on-rails - 为什么我需要在应用服务器上而不是仅在 Web 服务器上预编译 Rails Assets ?

Sinatra 与 NGINX 与 Unicorn 上的 Javascript 错误

ruby-on-rails - 如何使用 bootstrap 与 Rails 进行 react

ruby-on-rails - CanCanCanability.rb 中的用户为零

apache - a2ensite 'Site: ___ does not exist' 错误,即使使用 .conf 文件

java - 物理内存使用率过高

ruby-on-rails - 在 Heroku、Cedar 和 Unicorn 上:Getting ActiveRecord::StatementInvalid: PGError: SSL SYSCALL error: EOF detected