独角兽服务新贵脚本抛出“-su: bundle: command not found”

Unicorn service upstart script throws "-su: bundle: command not found"

我最近在 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 下的评估命令和它 return 有这个错误

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

好像脚本没有切换用户?这可能是当我启动 VPS.

时独角兽没有启动的原因

完整的独角兽暴发户脚本在这里:

#!/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: [=12=] <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 - `cat $PID`
}

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

case  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

更多相关信息

这里是 ruby、rails 和用户 joe 下的捆绑器的路径。在 root 下找不到它们。

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 用户下找不到捆绑器是有意义的,但 upstart 命令应该切换到用户 'joe' 到 运行 捆绑命令。这是我不明白的部分。

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

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

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

在 Ubuntu 上,我创建了文件 /etc/profile.d/rbenv.sh,内容如下:

export RBENV_ROOT=/home/YOUR_USER_PATH/.rbenv
export PATH=$RBENV_ROOT/shims:$RBENV_ROOT/bin:$PATH