简体   繁体   English

Unicorn服务upstart脚本抛出“-su:bundle:command not found”

[英]Unicorn service upstart script throws “-su: bundle: command not found”

I recently created a VPS on DigitalOcean to host a rails app. 我最近在DigitalOcean上创建了一个VPS来托管rails应用程序。 I followed their guide to setup Unicorn with my application. 我按照他们的指南用我的应用程序设置了Unicorn。 https://www.digitalocean.com/community/tutorials/how-to-deploy-a-rails-app-with-unicorn-and-nginx-on-ubuntu-14-04 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 . 我运行sudo service unicorn_appxyz start时出现问题。 The error given was -su: bundle: command not found 给出的错误是-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). 我跟踪了init.d脚本并在终端中粘贴了评估的服务器启动命令,并且在用户joe (安装rbenv的用户和应用程序的所有者)下执行时,它可以正常工作。 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. 我然后sudo su - 进入root用户并运行service unicorn_appxyz start错误当然是一样的。 Then I ran the evaluated command under root and it return with this error 然后我在root下运行了evaluate命令,它返回了这个错误

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. 这可能是我启动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. 这是用户joe下的ruby,rails和bundler的路径。 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. 有意义的是在root用户下找不到bundler,但upstart命令应该切换到用户'joe'来运行bundle命令。 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. 启动时root用户首先su - 进入rails用户(在本例中为'joe')然后执行bundle以启动unicorn。 rbenv is single user, only 'joe' has bundle installed. rbenv是单用户,只有'joe'安装了bundle。 The path to bundle is likely stored in my .bashrc file. bundle的路径可能存储在我的.bashrc文件中。 However .bashrc file which is not invoked by login in through su - and that caused the bundle not installed error. 但是.bashrc文件不是通过su登录而调用的 - 并且导致bundle未安装错误。

I included the paths related to rbenv in .profile. 我在.profile中包含了与rbenv相关的路径。 This way when root su - into 'joe' the paths are loaded. 这种方式当root su - into'joe'时,路径被加载。

On Unbutu I created I file /etc/profile.d/rbenv.sh with this content: 在Unbutu我用这个内容创建了我的文件/etc/profile.d/rbenv.sh:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM