Easy cron scheduling in Ruby.

You do Ruby. You are on *nix. You have a task you need to schedule using cron from your scripts. Later you schedule another task. After some time, and probably from another script, you want to unschedule/cancel the first task … How do you do it?

Fetch CronEdit

    sudo gem install cronedit

….and just do it:

    Crontab.Add  'agent1', '5,35 0-23/2 * * * echo agent1'
    Crontab.Add  'agent2', {:minute=>5, :command=>'echo 42'}
    Crontab.Remove 'agent1'

You can CRUD your tasks independently (based on the IDs you give them). The schedule can be in Hash notation, or the standard cron text definition.

UPDATE: The new version of library allows you to do some more tricks. First of all, you can manipulate groups of definitions (bulk/group operations). Besides modifications of the actual crontab, you can do now all operations upon files or in memory. And you can combine these ‘devices’ as needed. Let me show you a small example:

    fc = FileCrontab.new '/jobs/mail-delivery.cron'
    Crontab.Merge fc
    Crontab.Subtract fc

The previous snippet loads a group of cron definitions from file and merges them with the current system crontab. On the last line the same group of definitions is removed from the system crontab.

See documentation for more details and examples.
UPDATE: A related article discusses the library in context of scheduling of Ruby Agents using Cron.

In Ruby there are few schedulers out there, for example the most advanced: openwferu scheduler. They are great for many task, yet…. they are Ruby proprietary schedulers and the host ruby script needs to be running. If you need native cron scheduling, for whatever reason, go for CronEdit.

Leave a Reply

Your email address will not be published. Required fields are marked *