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
source share