如何将通过cron任务调用的Rails Rake任务输出迁移到Rails生产服务器输出?

huangapple go评论62阅读模式
英文:

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服务器的标准输出集成在一起,这是由于操作系统架构的简单性质所致。

我通过以下方式解决了我的问题:

  1. 在Rails控制器中调用Rake任务的目标方法。
  2. 通过Rails路由调用Rails控制器的方法。
  3. 通过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:

  1. Call the target method of the Rake task in a Rails controller.
  2. Call the method of the Rails controller via a Rails route.
  3. 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.

huangapple
  • 本文由 发表于 2020年1月3日 16:44:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/59575459.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定