[英]Output STDOUT to a file with ruby during execution
What is the best way to output the STDOUT to a file while a process is running? 在进程运行时将STDOUT输出到文件的最佳方法是什么? I made a test script called "runMyScript" and it has some echo statements with some sleeps in between.
我制作了一个名为“ runMyScript”的测试脚本,它包含一些回显语句,并且之间有一些睡眠。
I am using Rails and Sidekiq to run this worker: 我正在使用Rails和Sidekiq运行此工作程序:
class RunBuildWorker
include Sidekiq::Worker
sidekiq_options retry: false, backtrace: true
def perform(file_name)
IO.popen('runMyScript blah blah blah blah') do |io|
File.open(FILE_MANAGER.configuration_directory + file_name, 'a') do |file|
while (line = io.gets)
file.write line
end
end
end
end
end
Unfortunately, it isn't writing to the file until the process is complete. 不幸的是,直到过程完成,它才写入文件。 How would I go about writing to the file during execution?
在执行过程中如何写文件?
Update: 更新:
Here is what the script is currently. 这是脚本当前的内容。
COUNTER=0
while [ $COUNTER -le 5 ]
do
echo "The counter is $COUNTER"
sleep 1
COUNTER=$(( $COUNTER + 1 ))
done
I should mention this is just a test script so I can try to redirect output, there is another script that was written by another team that I'll actually be using in the future. 我应该提到这只是一个测试脚本,所以我可以尝试重定向输出,还有另一个脚本,它是由另一个团队编写的,将来我会实际使用。
What about 关于什么
system("runMyScript blah blah blah blah > #{FILE_MANAGER.configuration_directory + file_name}")
This redirects any output printed to stdout to your file (redirect is done on console-level). 这会将所有输出到stdout的输出重定向到您的文件(重定向是在控制台级别完成的)。 If you want to append to the file, simply replace
>
with >>
. 如果你要追加到该文件,只需更换
>
与>>
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.