2013-10-10 59 views
0

我有一個ruby sinatra應用程序,它有一個rake任務,它將使用活動記錄來檢查數據庫中的數據文件。我使用when gem來按計劃運行這個rake任務。這工作很好,一段時間後,幾天後它會失敗,看起來它無法寫入日誌文件。我有一個救援,將通過電子郵件發送我的錯誤和堆棧跟蹤,這就是我得到:Ruby Crontab Rake任務失敗,但只有當命令由crontab運行時

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' 

每當寶石看起來像這樣由創建的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['\''[email protected]'\''] --silent' 

是什麼奇怪的是,當我將這個命令放到命令行中時,它運行良好。即使我將用戶切換到root並運行它,它仍然可以毫無問題地運行。我只在crontab運行時發生錯誤。

+0

當「crontab」作業運行時,「PATH」的設置與剛剛登錄時不同。您需要確保在cron作業中設置所需的'PATH'項目。 – lurker

+0

嗨@mbratch,感謝您的評論。我嘗試將PATH設置爲我登錄時的相同值,但仍得到相同的結果。 – khartnett

+0

當你在命令行上運行它時,你是用'/ bin/bash ...'來做它嗎? – lurker

回答

0

找到這個答案後:

https://stackoverflow.com/a/14792306/1512654

我試圖設置RVM並記錄輸出。這似乎得到它的工作。在刪除多餘的部分後,我發現我只需要在cron任務結束時添加「> /tmp/cron_log.log」以使其工作。我不確定這個工作的原因。我在猜測cron工作是否遇到了STDOUT問題?