您好我正在创建一个用于监控ruby程序的新init脚本。
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
我的监控过程是
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
但是当我在我的monit中执行启动服务时状态为“执行失败”并且我检查了monit的日志文件,它说
info : 'differ' start: /bin/bash
error : 'differ' failed to start
error : 'differ' process is not running
当我分析问题的根源时。原因是monit以root身份运行,执行ruby的脚本将作为 sudo /etc/init.d/differ.sh start 执行,但ruby仅安装在用户'amer'中。我试过了
sudo -u amer $EXEC >>init_log/output.log &
它将错误显示为
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>'
请帮助解决这个问题。我有两个红宝石版本。
/home/amer/.rvm/rubies/ruby-2.0.0-p247/bin/ruby
/home/amer/.rvm/bin/ruby
答案 0 :(得分:2)
看起来你的环境不见了。取代
sudo -u amer $EXEC >>init_log/output.log &
与
su -s /bin/bash - amer -c "$EXEC >> init_log/output.log 2>&1" &
这应该正确设置您的shell环境。如果之前运行过sudo .. >> log
,则日志文件可能由root拥有。改变它或它会失败。我还添加了STDERR到STDOUT的重定向,因为您可能对错误消息感兴趣。
答案 1 :(得分:1)
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)使用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&
它对我有用。希望它适合每个人。
答案 2 :(得分:0)
以下是我想在init中运行ruby脚本时的操作:
我切换到超级用户并安装rvm。这不会导致单用户安装出现问题。
我把它放在init脚本中:
/usr/local/rvm/bin/rvm-shell 'yourgemset' -c 'ruby pathtoyourscript/yourscript.rb'
示例:
/usr/local/rvm/bin/rvm-shell 'jruby-1.7.4' -c 'ruby /home/someone/service.rb'
提示:需要在该gemset中安装所有必需的gem。
执行所有这些操作的正确方法是创建一个rvm包装器脚本(see example),但我发现我的方法更容易进行简单的设置,其中没有很多gemset。