Domo Arigato Mr. Cron-boto

Cron is a utility for scheduling console commands. It enables you to setup recurring tasks the same way you plan meetings in Outlook. Need to backup server logs on the third Wednesday of every month at 10:45pm? No problem, add an rsync job to your crontab and you’re all set!

Now here’s the rub. If you’re just getting started with cron, writing patterns - the actual scheduling logic - is a bit cryptic. For example, the cron pattern for that rsync backup is: 45 22 15-21 * Wed It’s easier to decipher than regex, but some more context would be helpful.

To build confidence and really master cron, check out Cron::Parser from Vishal Telangre. This Ruby gem translates patterns into a more natural, sentence-like format and identifies errors & invalid inputs:

```ruby require ‘cron’

Define pattern, parse it, and print the ‘humanized’ version

# BTW, the third Wednesday of the month falls between the 15th and the 21st test = ‘45 22 15-21 * Wed’ pattern = Cron::Parser.new(test) puts pattern.meaning #=> at 45th minute; on 10pm; on days: 15th, 16th, 17th, 18th, 19th, 20th, 21st; every month; on Wednesday

Parse just the day of month field

puts pattern.day_of_month_field.meaning #=> on days: 15th, 16th, 17th, 18th, 19th, 20th, 21st

Throw an error for invalid patterns

Cron::Parser.new(‘45 22 15-21 0 Wed’) #=> investigate_invalid_values!': value: '0' not allowed for 'month' field, run: 'Cron::Parser::MonthField.allowed_values' to know valid values (InvalidMonthFieldError) ``

In my experience, the best startups encourage communication. That’s why I send Engineering & Product an email illustrating our sales pipeline every day. But instead of manually running SQL reports, digging through my homebrew CRM, and formatting data in Gmail, I wrote a cron job to do it for me every weekday at 7:15pm.

Minutes spent coding: 45

Hours saved since 2011: 200+

Number of missed emails: Zero

Join the discussion

2017 Neal Shyam