由于root用户无法访问Ruby,因此无法使用sudo启动服务 [英] Can't start service with sudo since root user has no access to Ruby

查看:123
本文介绍了由于root用户无法访问Ruby,因此无法使用sudo启动服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

tl; dr

试图运行需要运行ruby的服务。但是,Ruby是通过 RVM 安装的,root用户似乎无法访问它,从而产生错误 / usr / bin / env:ruby:没有这样的文件或目录 rvmsudo 不起作用。

tl;dr
Trying to run a service which needs ruby to run. But, Ruby is installed with RVM where the root user can't seem to access it, producting the error /usr/bin/env: ruby: No such file or directory. rvmsudo doesn't work.

背景

我具有 init.d 脚本,该脚本应该启动 unicorn服务器。我将脚本保存在我的rails应用程序的 config 目录中,并从 /etc/init.d/busables_unicorn

Background
I have an init.d script which is supposed to start a unicorn server. I keep the script in the config directory of my rails application and symlink to it from /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

此脚本(实际上,它只是运行以下命令:

This script (which is appended to the bottom) essentially just runs the following command:

$APP_ROOT/bin/unicorn -D -c $APP_ROOT/config/unicorn.rb -E production

其中 $ APP_ROOT 是我的rails应用程序根目录的路径。每次在该init.d脚本中执行该命令时,都应该以 dtuite (我的部署)用户身份执行。为此,我将 su -c $ CMD-dtuite 称为 $ CMD

where $APP_ROOT is the path to the root of my rails application. Every time that command is executed in that init.d script, it is supposed to do so as the dtuite (my deploy) user. To accomplish that, I call su -c "$CMD" - dtuite rather than just $CMD.

/ bin / unicorn 是由捆绑软件 config / unicorn.rb 包含一些传递给它的配置选项。

/bin/unicorn is a "binscript" which was generated by Bundler and config/unicorn.rb contains some configuration options which are passed to it.

独角兽脚本如下:

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

现在,我正在尝试通过运行以下命令来启动我的独角兽服务:

Now, I'm trying to start my unicorn service by running:

sudo service busables_unicorn start

但这会产生错误:

/usr/bin/env: ruby: No such file or directory

我相信这是因为我以root用户身份运行服务,但RVM已在<$ c下安装了ruby $ c> dtuite 用户的主目录,root用户无权访问。

I believe that this is happening because I'm running the service as the root user but RVM has installed ruby under the dtuite user's home directory and the root user has no access to it.

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
What do I need to do to make this work?

我的设置

-ubuntu 11.10

-红宝石1.9.3p0(2011-10-30修订版33570)[i686-linux]

-nginx:nginx版本:nginx / 1.0.5

My Setup
- ubuntu 11.10
- ruby 1.9.3p0 (2011-10-30 revision 33570) [i686-linux]
- nginx: nginx version: 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 may help but to be honest I don't really understand it.

附录

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 不会产生一个读取通常会设置rvm环境的配置文件的外壳。

It sounds like su isn't spawning a shell that reads the profile files that normally setup the rvm environment.

我会尝试更改命令运行到

I'd try changing the command you run to

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

这篇关于由于root用户无法访问Ruby,因此无法使用sudo启动服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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