【发布时间】:2013-10-23 23:42:09
【问题描述】:
我有一个 ruby sinatra 应用程序,它有一个 rake 任务,它将使用活动记录检查数据文件与数据库。我正在使用无论何时 gem 按计划运行此 rake 任务。这工作了一段时间,然后几天后它会失败,看起来它无法写入日志文件。我有一个救援会通过电子邮件向我发送错误和堆栈跟踪,这就是我得到的:
Broken pipe -
/usr/local/rvm/rubies/ruby-2.0.0-p195/lib/ruby/2.0.0/logger.rb:578:in `write'
/usr/local/rvm/rubies/ruby-2.0.0-p195/lib/ruby/2.0.0/logger.rb:578:in `warn'
/usr/local/rvm/rubies/ruby-2.0.0-p195/lib/ruby/2.0.0/logger.rb:578:in `rescue in write'
/usr/local/rvm/rubies/ruby-2.0.0-p195/lib/ruby/2.0.0/logger.rb:562:in `write'
/usr/local/rvm/rubies/ruby-2.0.0-p195/lib/ruby/2.0.0/logger.rb:382:in `add'
/usr/local/rvm/rubies/ruby-2.0.0-p195/lib/ruby/2.0.0/logger.rb:456:in `error'
/home/deployer/public/fulfillment_api/shared/bundle/ruby/2.0.0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract_adapter.rb:428:in `rescue in log'
/home/deployer/public/fulfillment_api/shared/bundle/ruby/2.0.0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract_adapter.rb:425:in `log'
/home/deployer/public/fulfillment_api/shared/bundle/ruby/2.0.0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:286:in `execute'
/home/deployer/public/fulfillment_api/shared/bundle/ruby/2.0.0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/mysql2_adapter.rb:220:in `execute'
/home/deployer/public/fulfillment_api/shared/bundle/ruby/2.0.0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/mysql2_adapter.rb:224:in `exec_query'
/home/deployer/public/fulfillment_api/shared/bundle/ruby/2.0.0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/mysql2_adapter.rb:233:in `select'
/home/deployer/public/fulfillment_api/shared/bundle/ruby/2.0.0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/database_statements.rb:24:in `select_all'
/home/deployer/public/fulfillment_api/shared/bundle/ruby/2.0.0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/query_cache.rb:63:in `select_all'
/home/deployer/public/fulfillment_api/shared/bundle/ruby/2.0.0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/database_statements.rb:30:in `select_one'
/home/deployer/public/fulfillment_api/shared/bundle/ruby/2.0.0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/database_statements.rb:36:in `select_value'
/home/deployer/public/fulfillment_api/shared/bundle/ruby/2.0.0/gems/activerecord-4.0.0/lib/active_record/relation/finder_methods.rb:174:in `exists?'
/home/deployer/public/fulfillment_api/shared/bundle/ruby/2.0.0/gems/activerecord-4.0.0/lib/active_record/querying.rb:3:in `exists?'
/home/deployer/public/fulfillment_api/releases/20131010141756/app/models/clients/test/order_file.rb:39:in `block in create_new_orders'
when gem 创建的 crontab 如下所示:
0 0,12 * * * /bin/bash -l -c 'cd /home/deployer/public/fulfillment_api/releases/20131010141756 && RACK_ENV=production bundle exec rake background_jobs:process_and_email['\''khartnet@gmail.com'\''] --silent'
奇怪的是,当我将此命令输入命令行时,它运行良好。即使我将用户切换到 root 并运行它,它仍然可以正常工作。只有在 crontab 运行时我才会收到错误消息。
【问题讨论】:
-
crontab作业运行时,PATH的设置与您刚登录时不同。您需要确保在 cron 作业中设置了所需的PATH项。 -
嗨@mbratch,感谢您的评论。我尝试将 PATH 设置为与登录时相同的值,但仍然得到相同的结果。
-
当你在命令行上运行它时,你是不是就这样用
/bin/bash...? -
是的,完全正确。这是一个复制和粘贴。我只是能够通过在命令末尾添加“>> /tmp/cron_log.log”来运行它。我不知道为什么会这样,但它可以正常工作。
标签: ruby activerecord cron