我想将我的输出重定向到 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
,并且stderr
到 stdout
,最终重定向到 /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/