2012-01-12 78 views
3

tl; dr
嘗試運行需要運行ruby的服務。但是,Ruby與RVM一起安裝,其中root用戶似乎無法訪問它,產生錯誤/usr/bin/env: ruby: No such file or directoryrvmsudo不起作用。無法使用sudo啓動服務,因爲root用戶無法訪問Ruby

背景
我有一個init.d腳本啓動unicorn server這是應該。我將腳本保存在我的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是路徑到我的導軌應用程序的根。每次在該init.d腳本中執行該命令時,都應該如dtuite(我的部署)用戶那樣執行。爲了做到這一點,我打電話su -c "$CMD" - dtuite,而不僅僅是$CMD

/bin/unicorn是由Bundlerconfig/unicorn.rb生成的「binscript」,它包含一些傳遞給它的配置選項。

麒麟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安裝了ruby根據dtuite用戶的主目錄和root用戶無權訪問它。

[email protected]:$ which ruby 
-> /home/dtuite/.rvm/rubies/ruby-1.9.3-p0/bin/ruby 
[email protected]:$ su 
Password: 
[email protected]st:$ which ruby 
[email protected]:$ 

問題
什麼我需要做的,使這項工作?

我設置
- ubuntu的11.10
- 紅寶石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-auto-ruby

$ 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 

This other question可以幫助,但說實話,我真的不明白。

附錄
在它的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 
+0

如果這是一個生產服務器,你不應該基於在用戶的目錄中使用Ruby。如果該用戶進行更改會發生什麼?在生產服務器上,我會安裝你需要的一個Ruby版本。而且,用戶不會擁有帳戶,只有管理員。 – 2012-01-12 21:29:58

+0

這是一個私人生產服務器。 'dtuite'就是我。 – 2012-01-12 21:49:54

回答

2

這聽起來像su不產卵一個外殼,讀取通常設置rvm環境的配置文件。

我會嘗試改變運行命令

source "/home/dtuite/.rvm/scripts/rvm" && $APP_ROOT/bin/unicorn... 
+0

這個和Andrei S的答案都有效。我先試了一下,所以我想我會接受這個。多謝你們。 – 2012-01-12 19:42:23

1

嘗試增加你的紅寶石路某處在啓動腳本的開始,在出口聲明是這樣的:

export PATH=/home/dtuite/.rvm/rubies/ruby-1.9.3-p0/bin:$PATH