tl;dr
尝试运行需要 ruby 才能运行的服务。但是,Ruby 安装的是RVM http://beginrescueend.com/root 用户似乎无法访问它,从而产生错误/usr/bin/env: ruby: No such file or directory
. rvmsudo
不起作用。
背景
我有一个init.d
应该启动的脚本独角兽服务器 http://unicorn.bogomips.org/。我将脚本保存在config
我的 Rails 应用程序的目录及其符号链接/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
where $APP_ROOT
是我的 Rails 应用程序根目录的路径。每次在 init.d 脚本中执行该命令时,都应该按照dtuite
(我的部署)用户。为了实现这一点,我打电话给su -c "$CMD" - dtuite
而不仅仅是$CMD
.
/bin/unicorn
是一个“binscript”,它是由Bundler http://gembundler.com/ and 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 已在以下位置安装了 rubydtuite
用户的主目录,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:$
Question
我需要做什么才能使这项工作成功?
My Setup
- Ubuntu 11.10
- ruby 1.9.3p0(2011-10-30 修订版 33570)[i686-linux]
- nginx:nginx版本:nginx/1.0.5
我尝试过的
rvmsudo https://stackoverflow.com/questions/3644897/rvm-cannot-use-ruby-with-sudo
$ rvmsudo service busables_unicorn start
/usr/bin/env: ruby: No such file or directory
rvm 自动 ruby https://stackoverflow.com/questions/8819096/how-do-i-reference-the-local-ruby-in-a-hash-bang-executable
$ 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
这另一个问题 https://stackoverflow.com/questions/3230404/rvm-and-thin-root-vs-local-user可能有帮助,但说实话我不太明白。
Appendix
The 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