Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing crontab-style lines

Tags:

python

crontab

I need to parse a crontab-like schedule definition in Python (e.g. 00 3 * * *) and get where this should have last run.

Is there a good (preferably small) library that parses these strings and translates them to dates?

like image 543
Krumelur Avatar asked Sep 12 '11 15:09

Krumelur


People also ask

What does 0 * * * * mean in crontab?

0 * * * * -this means the cron will run always when the minutes are 0 (so hourly) 0 1 * * * - this means the cron will run always at 1 o'clock. * 1 * * * - this means the cron will run each minute when the hour is 1.

How do you read a cron expression?

A cron expression is a string consisting of six or seven subexpressions (fields) that describe individual details of the schedule. These fields, separated by white space, can contain any of the allowed values with various combinations of the allowed characters for that field.

What is crontab guru?

The quick and simple editor for cron schedule expressions by Cronitor.


2 Answers

Perhaps the python package croniter suits your needs.

Usage example:

>>> import croniter >>> import datetime >>> now = datetime.datetime.now() >>> cron = croniter.croniter('45 17 */2  * *', now) >>> cron.get_next(datetime.datetime) datetime.datetime(2011, 9, 14, 17, 45) >>> cron.get_next(datetime.datetime) datetime.datetime(2011, 9, 16, 17, 45) >>> cron.get_next(datetime.datetime) datetime.datetime(2011, 9, 18, 17, 45) 
like image 129
HAL Avatar answered Sep 24 '22 01:09

HAL


Maybe you can use this module:

http://code.activestate.com/recipes/577466-cron-like-triggers/

I used that module for making an user-space cron in Python and it works very well. This module can handle crontab-like lines.

like image 37
Diego Navarro Avatar answered Sep 26 '22 01:09

Diego Navarro