tl;dr
正在尝试运行需要ruby才能运行的服务。但是,Ruby安装了RVM,根用户似乎无法访问它,从而产生错误/usr/bin/env: ruby: No such file or directory
。rvmsudo
不起作用。
背景
我有一个init.d
脚本,它应该启动一个独角兽服务器。我将脚本保存在rails应用程序的config
目录中,并从/etc/init.d/busables_unicorn
进行符号链接。
$ ls -l /etc/init.d/busables_unicorn
-> lrwxrwxrwx 1 root root 62 2012-01-12 15:02 busables_unicorn -> /home/dtuite/dev/rails/busables/current/config/unicorn_init.sh
这个脚本(附加在底部)本质上只是运行以下命令:
$APP_ROOT/bin/unicorn -D -c $APP_ROOT/config/unicorn.rb -E production
其中$APP_ROOT
是到rails应用程序的根的路径。每次在init.d脚本中执行该命令时,都应该以dtuite
(我的部署)用户的身份执行。为了实现这一点,我调用su -c "$CMD" - dtuite
而不仅仅是$CMD
。
/bin/unicorn
是Bundler生成的"binscript",config/unicorn.rb
包含一些传递给它的配置选项
独角兽binscript看起来像这样:
#!/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')
现在,我正试图通过运行来启动我的独角兽服务
sudo service busables_unicorn start
然而,这会产生错误:
/usr/bin/env: ruby: No such file or directory
我相信发生这种情况是因为我以root用户的身份运行服务,但RVM在dtuite
用户的主目录下安装了ruby,而root用户无法访问它
dtuite@localhost:$ which ruby
-> /home/dtuite/.rvm/rubies/ruby-1.9.3-p0/bin/ruby
dtuite@localhost:$ su
Password:
root@localhost:$ which ruby
root@localhost:$
问题
我需要做些什么才能让它发挥作用?
我的设置
-ubuntu 11.10
-ruby 1.9.3p0(2011-10-30修订版33570)[i686-linux]
-nginx:nginx版本:nginx/1.0.5
我尝试过的
rvmsudo
$ rvmsudo service busables_unicorn start
/usr/bin/env: ruby: No such file or directory
rvm自动红宝石
$ sudo service cakes_unicorn start
-> [sudo] password for dtuite:
-> -su: /home/dtuite/dev/rails/cakes/current/bin/unicorn: rvm-auto-ruby: bad interpreter: No such file or directory
这个问题可能会有所帮助,但老实说,我真的不明白
附录busables_unicorn
脚本的整体。
# INFO: This file is based on the example found at
# https://github.com/defunkt/unicorn/blob/master/examples/init.sh
# Modifications are courtesy of Ryan Bate's Unicorn Railscast
# Install Instructions:
# sudo ln -s full-path-to-script /etc/init.d/APP_NAME_unicorn
# Once installed, an app's unicorn can be reloaded by running
# sudo service APP_NAME_unicorn restart
#!/bin/sh
set -e
# Example init script, this can be used with nginx, too,
# since nginx and unicorn accept the same signals
# Feel free to change any of the following variables for your app:
TIMEOUT=${TIMEOUT-60}
APP_ROOT=/home/dtuite/dev/rails/busables/current
PID=$APP_ROOT/tmp/pids/unicorn.pid
# in order to access this, we need to first run
# 'bundle install --binstubs'. THis will fill our
# app/bin directory with loads of stubs for executables
# this is the command that is run when we run this script
CMD="$APP_ROOT/bin/unicorn -D -c $APP_ROOT/config/unicorn.rb -E production"
# we don't need an init config because this file does it's job
action="$1"
set -u
old_pid="$PID.oldbin"
cd $APP_ROOT || exit 1
sig () {
test -s "$PID" && kill -$1 `cat $PID`
}
oldsig () {
test -s $old_pid && kill -$1 `cat $old_pid`
}
case $action in
start)
sig 0 && echo >&2 "Already running" && exit 0
# NOTE: We have to change all these lines.
# Otherwise, the app will run as the root user
su -c "$CMD" - dtuite
;;
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"
su -c "$CMD" - dtuite
;;
upgrade)
if sig USR2 && sleep 2 && sig 0 && oldsig QUIT
then
n=$TIMEOUT
while test -s $old_pid && test $n -ge 0
do
printf '.' && sleep 1 && n=$(( $n - 1 ))
done
echo
if test $n -lt 0 && test -s $old_pid
then
echo >&2 "$old_pid still exists after $TIMEOUT seconds"
exit 1
fi
exit 0
fi
echo >&2 "Couldn't upgrade, starting '$CMD' instead"
su -c "$CMD" - dtuite
;;
reopen-logs)
sig USR1
;;
*)
echo >&2 "Usage: $0 <start|stop|restart|upgrade|force-stop|reopen-logs>"
exit 1
;;
esac
听起来su
并没有生成一个shell来读取通常设置rvm环境的配置文件。
我会尝试将你运行的命令更改为
source "/home/dtuite/.rvm/scripts/rvm" && $APP_ROOT/bin/unicorn...
尝试在启动脚本开头的某个位置添加您的ruby路径,导出语句如下:
export PATH=/home/dtuite/.rvm/rubies/ruby-1.9.3-p0/bin:$PATH