Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ruby plugin/gem to convert cron into a human readable format

Is there a ruby gem/plugin which will convert something like */10 * * * 1,3 to "Triggers every 10 minutes on Monday, Wednesday" ?

like image 849
ed1t Avatar asked Apr 21 '11 14:04

ed1t


1 Answers

There's nothing I know of and I also didn't find anything with Google. You may be able to hack something together on your own though:

>> cron = "*/10 * * * 1,3 foo" 
#=> "*/10 * * * 1,3 foo"
>> min, hour, dom, month, dow, command = cron.split 
#=> ["*/10", "*", "*", "*", "1,3", "foo"]

Once you have the vars, you can start assembling the parts for your output:

>> require 'date' 
#=> true
>> dow.split(/,/).map { |day| Date::DAYNAMES[day.to_i] } 
#=> ["Monday", "Wednesday"]
>> min.start_with?('*') ? "every #{min.split('/')[1]} minutes" : "#{min} past" 
#=> "every 10 minutes"
>> min = '5' 
#=> "5"
>> min.start_with?('*') ? "every #{min.split('/')[1]} minutes" : "#{min} past" 
#=> "5 past"

Obviously that's just some rough ideas (for example you may want a regex with capture groups for parsing the entry), but since the crontab entries are well specified, it shouldn't be too hard to come up with something that works for most of the entries you are likely to encounter.

like image 164
Michael Kohl Avatar answered Oct 03 '22 17:10

Michael Kohl