Ruby on Rails - Rake 任务无法通过 Cron 运行
当我手动执行 rake 任务时,它工作正常,但是当我在 Cron 中输入相同的命令时,没有任何反应:
cd /path/to/my/rails/app && rake daily_import
Cron 日志表明该命令已发出:
CMD (cd /path/to/my/rails/app && rake daily_import)
rake 任务记录错误和成功消息,但没有任何内容记录到日志中,什么也没做。但是,如果我使用同一用户复制并粘贴 CMD 文本,则 Cron 正在运行该命令,一切正常。
我假设在 Cron 中运行任务应该与我自己输入任务相同,这是正确的吗?
When I execute a rake task manually, it works fine but when I put the same command in Cron nothing happens:
cd /path/to/my/rails/app && rake daily_import
The Cron log indicates that the command was issues:
CMD (cd /path/to/my/rails/app && rake daily_import)
The rake task logs error and success messages, but nothing is recorded to the log, nothing is done at all. However if I copy and paste the text of the CMD with the same user Cron is running the command in everything works fine.
I'm assuming that running a task in Cron should be the same as typing it in myself, is this correct?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这件事对我有用
你需要在任务之前指定 /bin/bash -l -c 。
This thing worked for me
You need to specify /bin/bash -l -c before your task.
查找 cron 守护程序可能已发送给运行 cron 作业的用户的邮件。如果 cron 作业在 stderr 或 stdout 上生成输出,则 cron 守护程序会将其通过电子邮件发送给 cron 作业的所有者。如果出现问题(可能是由于 PATH 问题,如上面 Rob 建议的那样),您可能会在来自 cron 守护程序的电子邮件中看到有用的错误消息。
Look for mail that the cron daemon might have sent to the user under which the cron job is running. If a cron job produces output on stderr or stdout, the cron daemon will email that to the owner of the cron job. If something is going wrong (possibly because of a PATH issue, like Rob suggests above), you might see a helpful error message in an email from the cron daemon.
如果有人使用 Rails 3.2<>4.0 查看此内容,我必须在我的 crontab 中使用此内容:
In case anyone is looking at this with Rails 3.2<>4.0, I had to use this in my crontab:
以下解决方案适用于我的 Rails 3 和 rvm 集成。我只是执行一个 bash 脚本:
如果没有
/bin/bash -l -c
部分,这似乎不起作用。然后,根据 rvm 文档,我的 bash 脚本如下:
The following solution worked for me for Rails 3 and rvm integration. I simply execute a bash script:
This doesn't seem to work without the
/bin/bash -l -c
part.Then, my bash script is as follows, according to the rvm documentation:
rake 是否在用于运行此 shell 的 cron 用户的路径中?
Is rake in the PATH of the cron user for running this shell?
我使用以下格式来运行 rake 任务,路径更加明确一些,
我还喜欢将输出重定向到文件,以便我可以检查错误
I use the following format to run rake tasks, a little more explicit with the paths
I also like to redirect output to a file so I can check the errors