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

时间:2014-01-20 10:14:01

标签: 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 puts 'done with step 1' else puts 'running generator'") do
    generator.rb
end

在跑步的过程中我发现错误没有参数。

这是堆栈跟踪: -

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 :(得分:0)

我会使用rufus-scheduler作为

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