How to run ruby ​​through sudo

Hey. I am creating a new init script to monitor a ruby ​​program.

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

  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 start the launch service in my monit, the status was "Execution failed", and I checked the monit log file, which said

  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 that monit runs as root, and the ruby ​​script will run as sudo / etc / init.d / differ.sh start , but ruby ​​is installed only in user amer. I tried

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

he 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 with this problem. I have two versions of Ruby.

  /home/amer/.rvm/rubies/ruby-2.0.0-p247/bin/ruby /home/amer/.rvm/bin/ruby 
+4
source share
3 answers

after a long struggle, I found a solution to this problem. Two things need to be done.

1) EXPORT PATH, GEM_HOME, GEM_PATH in script

 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)

  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 will be for everyone.

+1
source

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 properly configure the shell environment. If you run sudo .. >> log earlier, the sudo .. >> log file may be owned by root. Change it, otherwise it will fail. I also added STDERR redirection to STDOUT, since you are probably interested in error messages.

+2
source

Here is what I do when I want to run ruby ​​scripts in init:

I switch to superuser and install rvm. This will not cause problems with the installation of a single user.

I put this in an init script:

 /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 necessary stones must be installed in this gemset.

The right way to do this is to create a rvm script wrapper ( see example ), but I find that my method is easier for a simple setup where there are not many gems.

0
source

Source: https://habr.com/ru/post/1493187/


All Articles