Kreeki
Kreeki

Reputation: 3732

Perform a script automatically every <time a day I set>

I would like to run for example a synchronizing ruby script (in rails action if it's possible) between two databases every day at 3AM. It'd would be nice to have those times stored in a database, because if the synchonization fails I'd like to set it to try it again in an hour (and in database I can easily change it to every day at 5AM if I'd wanted to, even other admins in administration of my application). I would like to have the script in one of actions in my Rails application, so I can create a page in administration where just by clicking the link the script would run (not necessary, it can be in a separate Ruby file, which action would call eventually, but it would be nicer to have it as actions in a dedicated controller, because I'll have many scripts like this one and I just like having similar things in a one place). Is this somehow possible without things-I-dont-know-anything-about-yet like cron?

Upvotes: 1

Views: 2950

Answers (2)

dhofstet
dhofstet

Reputation: 9964

If you don't want to deal directly with cron, have a look at whenever. It provides a nice syntax for writing cron jobs:

every :hour do
  runner "MyModel.do_something"
end

Upvotes: 7

Dan Grossman
Dan Grossman

Reputation: 52372

You would use a task scheduler to do this.

On Windows, the standard scheduler is called Task Scheduler.

On Linux, the standard scheduler is called cron.

Both are trivially simple to use. If you're writing Ruby code, you can schedule a task.

For example, to schedule a command to run every hour of every day with cron, type crontab -e, and add this line:

0 3 * * * /command/to/run

To have it run every hour, so that you can have it check a database to see if it should do something:

0 * * * * /command/to/run

Upvotes: 3

Related Questions