Ruby 如何在运行程序时运行一个程序并定期检查输出是否存在

Ruby 如何在运行程序时运行一个程序并定期检查输出是否存在,ruby,Ruby,我想运行一个正在生成一些文件的脚本,并希望每3分钟检查一次脚本生成文件的存在性 我做了以下几件事 def periodic_method time, block t = EventMachine::PeriodicTimer.new(time) {eval(block)} begin yield ensure t.cancel end end periodic_method(60, "if File.exists(file1.txt) then

我想运行一个正在生成一些文件的脚本,并希望每3分钟检查一次脚本生成文件的存在性

我做了以下几件事

def periodic_method time, block
   t = EventMachine::PeriodicTimer.new(time) {eval(block)}
   begin
      yield
   ensure
      t.cancel
   end
end

periodic_method(60, "if File.exists(file1.txt) then puts 'done with step 1' else puts 'running generator'") do
    generator.rb
end
在运行时,我没有错误的参数

以下是stacktrace:-

wrong number of arguments (1 for 0)
/tools/simulation/simulation_assemble/test.rb:652:in `eval'
(eval):1:in `block in periodic_block'
/tools/simulation/simulation_assemble/test.rb:652:in `eval'
/tools/simulation/simulation_assemble/test.rb:652:in `block in periodic_block'
/tool/pandora64/.package/ruby-1.9.2-p136/lib/ruby/gems/1.9.1/gems/eventmachine-1.0.0.beta.4
/lib/em/timers.rb:52:in `call'
/tool/pandora64/.package/ruby-1.9.2-p136/lib/ruby/gems/1.9.1/gems/eventmachine-1.0.0.beta.4/lib/em/timers.rb:52:in `fire'
/tool/pandora64/.package/ruby-1.9.2-p136/lib/ruby/gems/1.9.1/gems/eventmachine-1.0.0.beta.4/lib/eventmachine.rb:206:in `call'
/tool/pandora64/.package/ruby-1.9.2-p136/lib/ruby/gems/1.9.1/gems/eventmachine-1.0.0.beta.4/lib/eventmachine.rb:206:in `run_machine'
/tool/pandora64/.package/ruby-1.9.2-p136/lib/ruby/gems/1.9.1/gems/eventmachine-1.0.0.beta.4/lib/eventmachine.rb:206:in `run'
generator.rb:195:in `block in run'
generator.rb:168:in `standard_exception_handling'
generator:191:in `run'
generator:28:in `< main>'
参数数目错误(1代表0)
/工具/模拟/模拟装配/测试.rb:652:in'eval'
(eval):1:在“周期块中的块”中
/工具/模拟/模拟装配/测试.rb:652:in'eval'
/工具/模拟/模拟装配/测试。rb:652:in‘block in periodic_block’
/tool/pandora64/.package/ruby-1.9.2-p136/lib/ruby/gems/1.9.1/gems/eventmachine-1.0.0.beta.4
/lib/em/timers.rb:52:in'call'
/tool/pandora64/.package/ruby-1.9.2-p136/lib/ruby/gems/1.9.1/gems/eventmachine-1.0.0.beta.4/lib/em/timers.rb:52:in'fire'
/tool/pandora64/.package/ruby-1.9.2-p136/lib/ruby/gems/1.9.1/gems/eventmachine-1.0.0.beta.4/lib/eventmachine.rb:206:在'call'中
/tool/pandora64/.package/ruby-1.9.2-p136/lib/ruby/gems/1.9.1/gems/eventmachine-1.0.0.beta.4/lib/eventmachine.rb:206:在“运行机器”中
/tool/pandora64/.package/ruby-1.9.2-p136/lib/ruby/gems/1.9.1/gems/eventmachine-1.0.0.beta.4/lib/eventmachine.rb:206:在“运行”中
发电机。rb:195:“运行中的块”
生成器.rb:168:“标准异常处理”中的
发电机:191:处于“运行”状态
生成器:28:在“
”中
有人能帮我完成任务吗

有没有其他方法来完成这项任务?

我会用它


请先格式化你的代码!!你到底得到了什么错误?我得到了这个错误“参数数目错误(1代表0)”@anamika:post-stack trace,等等。我想同时执行这两个任务?这不会在命令
ruby generator.rb
返回之前放置“完成生成…”。那么您想在生成器完成之前检查文件是否存在?我想将第二个任务(检查文件是否存在)作为一个任务。您可以添加另一个调度程序。每隔“3m”。它将在另一个线程中完成。
require 'rufus-scheduler'

scheduler = Rufus::Scheduler.new

scheduler.every '3m' do
  # run script here; would this do?
  puts "running generator..." 
  `ruby generator.rb`
  puts "done generating..."
  # now when the script is done,
  # check if File.exists?
end

scheduler.every '3m' do
  # do other things in another thread
end

scheduler.join

sleep 1 while true # in order for your program to stay alive