You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Docker容器中获取Whenever Gem任务日志?

Docker容器中无法获取Whenever Gem任务日志的问题解决

问题详情

我使用Whenever Gem定义了定时任务,代码如下:

task my_task: :environment do
  Rails.logger.info 'start task'
  MyModel.call_some_method
end

任务实际运行正常(可通过数据库表变更确认),但执行docker logs containerId查看容器日志时,只能看到[write] crontab file updated,无法获取任务中输出的日志内容。

我已经尝试过两种无效方案:

  • 注释schedule.rb中的:output配置
  • 设置set :output, '/dev/stdout'

容器相关配置如下:
Dockerfile:

...

ENTRYPOINT ["/rails/bin/docker-cron-entrypoint"]

CMD ["cron", "-f"]

docker-cron-entrypoint脚本内容:

#!/bin/bash -e
bundle exec whenever --update-crontab
exec "${@}"

解决方法

在schedule.rb中设置日志输出到容器主进程的标准输出:

set :output, '/proc/1/fd/1'

内容的提问来源于stack exchange,提问作者user1929618

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 07:37:42