[英]How to log script output to STDOUT and a file
我有以下Ruby块:
ruby_block "Validate" do
block do
require "mixlib/shellout"
begin
cmd = Mixlib::ShellOut.new("/usr/local/bin/someScript.py", :timeout => 3600)
cmd.live_stream = STDOUT
cmd.run_command
cmd.error!
rescue Exception => e
puts "Action failed..."
return 168
end
end
action :create
notifies :create, "ruby_block[Validated]", :immediately
not_if { node[:state][:validated] == true }
end
我想将脚本的结果记录到STDOUT和一个名为“ /tmp/xml_diff_results.txt”的文件中。
我所做的第一件事是更改:
cmd=Mixlib::ShellOut.new("/usr/local/bin/someScript.py", :timeout => 3600)
至:
cmd=Mixlib::ShellOut.new("/usr/local/bin/someScript.py > /tmp/xml_diff_results.txt", :timeout => 3600)
但是,这并没有达到我的预期。
然后我注意到了cmd.live_stream
变量。 有没有办法我可以利用它做类似的事情?:
cmd.live_stream = (STDOUT > /tmp/xml_diff_results.txt)
解:
解决我的问题的方法很简单,并受到@tensibai的启发。
log_file = File.open('/tmp/chef-run.log', File::WRONLY | File::APPEND)
LOG = Logger.new(log_file)
def shell(command)
LOG.info "Execute: #{command}"
cmd = Mixlib::ShellOut.new(command, :timeout => 1800)
cmd.run_command
LOG.info "Returned: #{cmd.stdout}"
cmd.error!
cmd
end
这不是Ruby甚至Chef的问题。 更像是Bash问题
一种运行命令并将其输出重定向到stdout和文件的方法是使用tee
echo 'Hello World!' | tee output.log
因此,以您的示例为例
cmd=Mixlib::ShellOut.new("/usr/local/bin/someScript.py | tee /tmp/xml_diff_results.txt", :timeout => 3600)
如果tee
不可用(窗口),则Ruby中的另一个选项(仅是内部):
cmd = Mixlib::ShellOut.new("/usr/local/bin/someScript.py", :timeout => 3600)
cmd.live_stream = STDOUT
cmd.run_command
# new part
log=::Tempfile.new(["xml_diff_results",".txt"])
errlog=::File.open(log.path.gsub(".txt",".err")
log.write(cmd.stdout)
errlog.write(cmd.stderr)
log.close
errlog.close
Chef::Log.info("Log results are in #{log.path}")
# end of new part
cmd.error!
将Chef::Log
级别更改为warn
如果您运行不带-l info
chef-client并且确实希望在chef日志中打印路径。
主要优点是它与平台无关,缺点是仅在命令结束执行后才写入日志文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.