Rake task via cron rubygems loading problem

I managed to get the cron job to run the rake task by doing the following:

cd /home/myusername/approotlocation/ && /usr/bin/rake sendnewsletter RAILS_ENV=development 

I checked with which ruby and which rake to make sure the paths are correct (from bash)

the work looks like it wants to start when I receive the next email from the cron daemon when it completes

 Missing these required gems: chronic whenever searchlogic adzap-ar_mailer twitter gdata bitly ruby-recaptcha You're running: ruby 1.8.7.22 at /usr/bin/ruby rubygems 1.3.5 at /home/myusername/gems, /usr/lib/ruby/gems/1.8 Run `rake gems:install` to install the missing gems. (in /home/myusername/approotlocation) 

my custom rake file in lib / tasks is as follows:

 task :sendnewsletter => :environment do require 'rubygems' require 'chronic' require 'whenever' require 'searchlogic' require 'adzap-ar_mailer' require 'twitter' require 'gdata' require 'bitly' require 'ruby-recaptcha' @recipients = Subscription.all(:conditions => {:active => true}) for user in @recipients Email.send_later(:deliver_send_newsletter,user) end end 

with or without elements, he still gives me the same error ...

Can anyone shed some light on this? or alternatively advise me how to create a custom file in a script directory that will run this function (I already have a cron job that will run and process all my delayed_jobs.

After Jonathan's suggestion below I ran

okr

as a cron job on my own and got the following output:

 SHELL=/bin/sh MAILTO=myemailaddress USER=myusername PATH=/usr/bin:/bin PWD=/home/myusername SHLVL=1 HOME=/home/myusername LOGNAME= myusername _=/usr/bin/env 

Does this mean that it is not loading ruby ​​files correctly? .... also took Jonathan's advice and released the following cron.sh file

 #!/bin/sh date echo "Executing Matenia Rake Task" PATH=/usr/bin:/bin cd /home/myusername/approotlocation/ rake sendnewsletter 

still get the missing gems ... Hooray!

+4
source share
3 answers

The easiest way to fix this (but kind of a gun) is with your shell type.

env | grep PATH

Then take this output and add it to crontab for this user

so it will look something like this.

 PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin # mh dom mon dow user command 42 6 * * * root job1 47 6 * * 7 root job2 52 6 1 * * root job3 

Just make sure your gems are inside this path.

+9
source

When cron starts, it runs with a minimal environment. Try running a cron job that simply executes env or which ruby , and you can see that your PATH does not match your interactive shell path. You will need to configure PATH in .bash_profile or another shell startup file.

0
source

I went around all this by creating my own script / file and executing it through cron. I don’t know how, but he managed to work ... Thanks for all the efforts.

0
source

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


All Articles