0

I would like to control my cron jobs through my administration page.
Basically I have my cronjobs in my database, and I would like to create my crontab "on the fly".

Here's an example:

require "#{RAILS_ROOT}/config/environment.rb"
Cron.all.each do |cron|
  if cron.at.blank?

    every eval(cron.run_interval) do
      cron.cmd
    end

  else

    every eval(cron.run_interval), :at => cron.time do
      cron.cmd
    end

  end
end

every 1.day do
  command "whenever --update-crontab"
end

But Whenever doesn't output any of the tasks inside the loop, only the "static" one.

0 0 * * * whenever --update-crontab

How can I make Whenever 'understand' my loop?

Frexuz
  • 4,732
  • 2
  • 37
  • 54

1 Answers1

1

You probably need to move your eval statement higher up, for example:

eval <<-EVAL
  every #{cron.run_interval} do
    #{cron.cmd}
  end
EVAL

Assuming that your cron.cmd is something like 'command "ls -a"'.

Pan Thomakos
  • 34,082
  • 9
  • 88
  • 85
  • Thanks so much, it worked! I tried <<-EOS, but I didn't know about <<-EVAL. :) – Frexuz Jan 06 '11 at 17:59
  • 1
    You can use any name you like, as long as it matches at the start and end of the string: <<-RANDOM_NAME ... RANDOM_NAME. The key is to have eval before it. – Pan Thomakos Jan 06 '11 at 18:14