英文:
How to port Rails Rake task output called via cron task to Rails production server output?
问题
我在crontab中有一系列的定时任务。这些定时任务通过以下格式调用Rake任务:
* * * * * /bin/sh -l -c 'cd /app && bundle exec rake foo:bar[baz] >> /var/log/foobar_sync.log 2>&1'
我原以为我通过2>&1
将它们的输出(通过puts
)重定向到了STDOUT,但我想我可能是错误地处理了这个问题。
我该如何将这些Rake任务的输出重定向到Rails服务器日志输出中,以及Rails服务器日志本身,以便它们的输出可以与Rails服务器输出一起查看?
英文:
I have a series of cron jobs in crontab. The cron jobs call Rake tasks via the following format:
* * * * * /bin/sh -l -c 'cd /app && bundle exec rake foo:bar[baz] >> /var/log/foobar_sync.log 2>&1'
I thought I was redirecting their output (via puts
) to STDOUT via 2>&1, but I suppose I am approaching the issue incorrectly.
How can I redirect the output of these Rake tasks into the Rails server log output, along with the Rails server logs themselves, so their output can be seen alongside Rails server output?
答案1
得分: 0
我确定除了可能的黑客方式外,没有办法将Rake任务的标准输出与Rails服务器的标准输出集成在一起,这是由于操作系统架构的简单性质所致。
我通过以下方式解决了我的问题:
- 在Rails控制器中调用Rake任务的目标方法。
- 通过Rails路由调用Rails控制器的方法。
- 通过crontab中的
curl
调用Rails路由。
现在方法的输出(在我的情况下,这是我创建的ActiveJob的同步任务)可以在Rails服务器的标准输出中看到。
英文:
I determined that there is no way (aside from hacking Linux, perhaps) to integrate the STDOUT of a Rake task into the STDOUT of a Rails server, due to the simple nature of the OS architecture.
I solved my issue by doing the following:
- Call the target method of the Rake task in a Rails controller.
- Call the method of the Rails controller via a Rails route.
- Call the Rails route via a
curl
in crontab.
Now the output of the method (in my case, a sync task for which I created an ActiveJob) is visible in the STDOUT of the Rails server.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论