简体   繁体   English

通过cron问题加载rubygems的rake任务

[英]rake task via cron problem loading rubygems

I have managed to get a cron job to run a rake task by doing the following: 我通过执行以下操作设法获得了一个运行rake任务的cron作业:

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) 我检查了which rubywhich rake以确保路径正确(来自bash)

the job looks like it wants to run as i get the following email from the cron daemon when it completes 该工作看起来像是要运行,因为我在完成后从cron守护程序中收到以下电子邮件

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: 我在lib / tasks中的自定义rake文件如下:

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. 或者建议我如何在脚本目录中创建一个自定义文件来运行这个功能(我已经有一个cron作业,它将运行并处理我所有的delayed_jobs。

After Jonathans suggestion below I ran 在乔纳森的建议下面,我跑了

env ENV

as a cron job on its own and received the following output: 作为一个cron工作,并收到以下输出:

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? 这是否意味着它没有正确加载ruby文件? .... also took Jonathans advice and produced the following cron.sh file ....还提出了Jonathans的建议,并制作了以下cron.sh文件

#!/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! 还有失踪的宝石通知......干杯!

Easiest way to fix this (but kind of a shotgun approche) is from your shell type 解决这个问题的最简单方法(但是一种霰弹枪的方法)来自你的shell类型

env | 环境| grep PATH grep PATH

Then take this output and add it your crontab for that user 然后获取此输出并将该crontab添加到该用户

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 只要确保你的宝石在那条路径内

When cron runs, it executes with a very minimal environment. 当cron运行时,它以非常小的环境执行。 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. 尝试运行一个只执行env或者which ruby的cron作业,你可能会发现你的PATH与你的交互式shell路径不同。 You'll need to specifically set the PATH in .bash_profile or another shell startup file. 您需要在.bash_profile或其他shell启动文件中专门设置PATH。

I got around all of this by making a custom script/file and running that through cron. 我通过制作自定义脚本/文件并通过cron运行它来解决所有这些问题。 I dont know how, but it managed to work ... Thanks for all the efforts. 我不知道如何,但它设法工作......感谢所有的努力。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM