views:

293

answers:

3

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

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

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

the job looks like it wants to run as i get the following 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 within 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 the require items, it still gives me the same error ...

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

After Jonathans suggestion below I ran

env

as a cron job on its own and received 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 it's not loading the ruby files properly? .... also took Jonathans advice and produced the following cron.sh file

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

still getting the missing gems notice ... Cheers!

A: 

When cron runs, it executes with a very minimal environment. Try running a cron job that just does env or which ruby, and you may see that your PATH is not the same as your interactive shell path. You'll need to specifically set the PATH in .bash_profile or another shell startup file.

Jonathan Julian
updated my post with ENV output ... maybe it would be better if I made a .sh script .... added that coding to above as well ...
Matenia Rossides
A: 

Easiest way to fix this (but kind of a shotgun approche) is from your shell type

env | grep PATH

Then take this output and add it your crontab for that user

so it would look something like this

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# m h dom mon dow user    command
42 6 * * *        root    job1
47 6 * * 7        root    job2
52 6 1 * *        root    job3

Just make sure your gems's are inside of that path

kSiR
There was alot of difficulty in updating the file with it inserting text where it shouldnt have been ... cannot complete this option unfortunately ... thanks for trying :)
Matenia Rossides
I still think this is a path issue, just using /usr/bin and /bin is a fairly small and narrow path. From YOUR shell type echo $PATH and this is what you should make the path.
kSiR
yeh i put that in the cron.sh file i wanted to run to do the rake task .. but unfortunately, it does not want to load the applications' installed gems ... i must be missing something soooo simple ...
Matenia Rossides
put the path at the top of your cron filecrontab -eadd your path, then your job and let me know how that works.
kSiR
as i said, i couldnt do this because the editor in the terminal was going nuts and not doing what i wanted it to do ... im guessing this would have worked ... so i will mark it as the solution.. but could not complete it due to editing difficulties (i guess i need to brush up on this before i do it again)
Matenia Rossides
A: 

I got around all of this by making a custom script/file and running that through cron. I dont know how, but it managed to work ... Thanks for all the efforts.

Matenia Rossides