简体   繁体   English

如何通过sudo运行ruby

[英]How to run ruby via sudo

Hi I am creating a new init script for monitoring a ruby program . 嗨,我正在创建一个新的init脚本来监控ruby程序。

NAME=differ
FILE_PATH=/home/amer/Documents/ruby_projects/differ/
PIDFILE=/home/amer/pid/differ.pid
PID=$$
EXEC='/home/amer/.rvm/rubies/ruby-2.0.0-p247/bin/ruby main_scheduler.rb'

do_start(){
    echo "started"
    cd $FILE_PATH
    pwd
    $EXEC  >> init_log/output.log &
    echo $! > $PIDFILE
    echo "---------"
    echo `cat $PIDFILE`
    echo "all are DONE "
}

do_stop(){
    PID=`cat $PIDFILE`
    echo $PID
    if ps -p $PID ; then
        kill -6  $PID
        echo "it is over"
    else
        echo "its not running"
    fi
}

case "$1" in
    start)
    echo $$
        echo -n "Starting script differ "
        do_start
        ;;
    stop)
        echo "stopping ...."
        do_stop
        ;;
    status)
        PID=`cat $PIDFILE`
        echo "STATUS $PID"
        if  ps -p $PID -f; then
            echo running
        else
            echo not running
        fi
        ;;
    restart|reload|condrestart)
        do_stop
        do_start
        ;;
    *)
        echo "Usage: /etc/init.d/blah {start|stop}"
        exit 1
        ;;
esac

exit 0

And my monit process is 我的监督过程是

 check process differ with pidfile /home/amer/pid/differ.pid
if changed pid then exec "/etc/init.d/differ start"
 start program = "/etc/init.d/differ start" 
 stop program = "/etc/init.d/differ stop"
 if 5 restarts within 5 cycles then timeout

But when I execute start service in my monit the status was "Execution failed" and i checked the log file of monit it said 但是当我在我的monit中执行启动服务时,状态是“执行失败”,我检查了它所说的monit的日志文件

       info     : 'differ' start: /bin/bash
       error    : 'differ' failed to start
       error    : 'differ' process is not running

When I analyzed the root of the problem . 当我分析问题的根源时。 the reason was monit is running as root and the script which executes ruby will be executing as sudo /etc/init.d/differ.sh start but ruby is installed only in user 'amer' . 原因是monit以root身份运行,执行ruby的脚本将以sudo /etc/init.d/differ.sh start的形式执行,但ruby仅安装在用户'amer'中。 I have tried 我试过了

 sudo -u amer $EXEC >>init_log/output.log &

it displayed the error as 它显示错误为

 amer@amer-Inspiron-1525:~$ /home/amer/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in `require': cannot load such file -- bundler/setup (LoadError)
from /home/amer/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in `require'
from main_scheduler.rb:2:in `<main>'

Please help in this problem . 请帮助解决这个问题。 I have two ruby versions. 我有两个红宝石版本。

     /home/amer/.rvm/rubies/ruby-2.0.0-p247/bin/ruby
     /home/amer/.rvm/bin/ruby

It looks like your environment is missing. 看起来你的环境不见了。 Replace 更换

sudo -u amer $EXEC >>init_log/output.log &

with

su -s /bin/bash - amer -c "$EXEC >> init_log/output.log 2>&1" &

This should setup your shell environment properly. 这应该正确设置您的shell环境。 If you ran sudo .. >> log before, the log file might be owned by root. 如果之前运行了sudo .. >> log ,则日志文件可能由root拥有。 Change that or it will fail. 改变它或它会失败。 I also added the redirect of STDERR to STDOUT, since you are probably interested in error messages. 我还添加了STDERR到STDOUT的重定向,因为您可能对错误消息感兴趣。

after a long struggle i found the solution for this problem . 经过长期的斗争,我找到了解决这个问题的方法。 Two things must be done 必须做两件事

1) EXPORT your PATH,GEM_HOME,GEM_PATH in the script 1)在脚本中导出PATH,GEM_HOME,GEM_PATH

export PATH="/home/amer/.rvm/gems/ruby-2.0.0-p247@rails329/bin:/home/amer/.rvm/gems/ruby-2.0.0-p247@global/bin:/home/amer/.rvm/rubies/ruby-2.0.0-p247/bin:/home/amer/.rvm/bin:/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games"   
export GEM_HOME=/home/amer/.rvm/gems/ruby-2.0.0-p247@rails329
export GEM_PATH=/home/amer/.rvm/gems/ruby-2.0.0-p247@rails329:/home/amer/.rvm/gems/ruby-2.0.0-p247@global

2) USE rvmsudo bundle exec ruby "filename" (use full path) 2)使用rvmsudo bundle exec ruby​​“filename”(使用完整路径)

 rvmsudo -u amer  /home/amer/.rvm/gems/ruby-2.0.0-p247@rails329/bin/bundle exec  /home/amer/.rvm/rubies/ruby-2.0.0-p247/bin/ruby main_scheduler.rb&

it worked for me . 它对我有用。 hope it does for everyone . 希望它适合每个人。

Here's what I do when I want to run ruby scripts in init: 这是我想在init中运行ruby脚本时的操作:

I switch to super user and install rvm. 我切换到超级用户并安装rvm。 This won't cause problems with your single user installation. 这不会导致单用户安装出现问题。

I put this in the init script: 我把它放在init脚本中:

/usr/local/rvm/bin/rvm-shell 'yourgemset' -c 'ruby pathtoyourscript/yourscript.rb'

Example: 例:

/usr/local/rvm/bin/rvm-shell 'jruby-1.7.4' -c 'ruby /home/someone/service.rb'

Hint: all the necessary gems need to be installed in that gemset. 提示:需要在该gemset中安装所有必需的gem。

The proper way of doing all this is to create an rvm wrapper script ( see example ) but I find my method easier for a simple setup where there aren't many gemsets. 这样做的正确方法是创建一个rvm包装器脚本( 参见示例 ),但我发现我的方法更容易进行简单的设置,其中没有很多gemsets。

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

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