独角兽服务新贵脚本抛出“-su: bundle: command not found"; [英] Unicorn service upstart script throws "-su: bundle: command not found"

查看:62
本文介绍了独角兽服务新贵脚本抛出“-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 &&捆绑执行独角兽 -c config/unicorn.rb -E 生产 -D

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

当前未安装程序bundle".您可以通过键入以下内容来安装它:apt-get 安装捆绑器

脚本好像没有切换用户?这可能是我启动 VPS 时独角兽无法启动的原因.

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

#!/bin/sh### 开始初始化信息# 提供:独角兽# 必需-开始:$all# 必需停止:$all# 默认开始:2 3 4 5# 默认停止:0 1 6# 简短说明:启动独角兽应用服务器# 描述:使用 start-stop-daemon 启动独角兽### 结束初始化信息设置 -eUSAGE="用法:$0 <开始|停止|重启|升级|旋转|强制停止>"# 应用设置用户=乔"APP_NAME="appxyz"APP_ROOT="/home/$USER/$APP_NAME"环境=生产"#环境设置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"# 确保应用存在cd $APP_ROOT ||出口 1签名(){test -s "$PID" &&kill -$1 `cat $PID`}旧签名(){test -s $OLD_PID &&kill -$1 `cat $OLD_PID`}案例 1 美元开始)sig 0 &&echo >&2已经在运行"&&退出 0echo "开始 $APP_NAME"su - $USER -c "$CMD";;停止)echo "停止 $APP_NAME"退出 &&退出 0echo >&2未运行";;强制停止)echo "强制停止 $APP_NAME"签署条款&&退出 0echo >&2未运行";;重新启动|重新加载|升级)sig USR2 &&回声重新加载 $APP_NAME" &&退出 0echo >&2 "无法重新加载,改为启动 '$CMD'"$CMD;;旋转)sig USR1 &&echo 旋转日志 OK &&退出 0echo >&2无法旋转日志"&&出口 1;;*)echo >&2 $USAGE出口 1;;esac

更多相关信息

这里是用户 joe 下 ruby​​、rails 和 bundler 的路径.在 root 下找不到它们.

joe@vps:~$ which ruby/home/joe/.rbenv/shims/rubyjoe@vps:~$ which rails/home/joe/.rbenv/shims/railsjoe@vps:~$ 哪个包/home/joe/.rbenv/shims/bundle

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

解决方案

我发现了这个问题.解释如下,

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

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

I recently created a VPS on DigitalOcean to host a rails app. I followed their guide to setup Unicorn with my application. https://www.digitalocean.com/community/tutorials/how-to-deploy-a-rails-app-with-unicorn-and-nginx-on-ubuntu-14-04

A problem occurred when I ran sudo service unicorn_appxyz start. The error given was -su: bundle: command not found

I traced the init.d script and pasted the evaluated server start up command in terminal and it works fine when executed under the user joe (the user which rbenv is install and the owner of the app). The evaluated command is

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

I then sudo su - into root user and ran service unicorn_appxyz start the error was of course the same. Then I ran the evaluated command under root and it return with this error

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

It seems the script is not switching the user? This is likely the cause of unicorn not starting when i boot my VPS.

The full unicorn upstart script is here:

#!/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

More related info

here are the paths for ruby, rails and bundler under user joe. Under root they are not found.

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

It make sense bundler could not be found under root user but the upstart command should have switched to user 'joe' to run the bundle command. This is the part I don't understand.

解决方案

I found out the issue. Explanation follows,

root user on startup will first su - into the rails user (in this case 'joe') then executes bundle to start up unicorn. rbenv is single user, only 'joe' has bundle installed. The path to bundle is likely stored in my .bashrc file. However .bashrc file which is not invoked by login in through su - and that caused the bundle not installed error.

I included the paths related to rbenv in .profile. This way when root su - into 'joe' the paths are loaded.

这篇关于独角兽服务新贵脚本抛出“-su: bundle: command not found";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆