ruby-on-rails - 将输出重定向到 stderr

标签 ruby-on-rails ruby redirect stderr

我想将我的输出重定向到 stderr。我有一个 cron 作业

function auth_tester {
  cd /data/$1/current && bundle exec rake 'authentication:tester' 1>    /dev/null
}

调用 rake 任务

namespace :authentication do

  desc "Automatically runs authentication tester and notifies in case of failure"
  task :tester => :environment do
    auth_tester_results = AuthenticationTester.perform

    exit(auth_tester_results.success? ? 0 : 1)
  end
end

如果“auth_tester_results” bool 值是假的,我希望将输出重定向到 stderr。如何做呢?

最佳答案

因为你已经在处理 shell,所以在 shell 中做:

function auth_tester {
  cd /data/$1/current && \
  bundle exec rake 'authentication:tester' >/dev/null 2>&1
  #                                 HERE:  ⇑⇑⇑⇑⇑⇑⇑⇑⇑⇑⇑⇑⇑⇑⇑
}

stderr 有一个 id = 2,我们将 stdout 重定向到 /dev/null,并且stderrstdout,最终重定向到 /dev/null

要将 stdout 重定向到 stderr,请使用相反的方法:

1>&2

要重定向 ruby​​ 的输出,请使用带有 IO#puts 的适当接收器:

$stderr.puts "Goes to stderr"

关于ruby-on-rails - 将输出重定向到 stderr,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41374373/

相关文章:

html - Ruby on Rails/HTML - 内联表单 :checked => false is not working

ruby-on-rails - 在 `includes` 急切加载上重用范围作为过滤器

ruby-on-rails - 使用多个参数问题创建

ruby - 导轨设置 : NoMethodError: undefined method `merge' for []:Array

ruby-on-rails - rails 3 项目中的 RSpec 和自动测试问题

.htaccess - 来自子文件夹的主机域

.htaccess - 尝试修复动态 URL 的 301 重定向 |不工作

php - symfony 验证路由是否被授予/评估返回 redirectToRoute 语句

ruby-on-rails - 为新应用扩展 rails new generator

ruby-on-rails - Rails 4 - 嵌套属性的问题