在python中解析和使用crontab调度
crontab的Python项目详细描述
版权所有2011-2016 Josiah Carlson
在lgpl许可证版本2.1和版本3下发布(您可以选择 你想把它绑在下面)。
说明
这个包打算提供一种解析crontab调度项的方法,并且 确定项目下一次运行的时间。更具体地说,它计算 从调用.next()方法到项 下一个应该被执行。
将下表与http://en.wikipedia.org/wiki/Cron#CRON_expression进行比较 您将注意到不支持w和符号。
Field Name | Mandatory | Allowed Values | Default Value | Allowed Special Characters |
---|---|---|---|---|
Seconds | No | 0-59 | 0 | * / , - |
Minutes | Yes | 0-59 | N/A | * / , - |
Hours | Yes | 0-23 | N/A | * / , - |
Day of month | Yes | 1-31 | N/A | * / , - ? L |
Month | Yes | 1-12 or JAN-DEC | N/A | * / , - |
Day of week | Yes | 0-6 or SUN-SAT | N/A | * / , - ? L |
Year | No | 1970-2099 | * / , - |
如果cron条目有5个值,则使用分钟/天/周,默认秒数 是,并附加默认年份。如果cron条目有6个值,每年分钟 已使用,默认秒数已提前。
因此,只有5-7个值的crontab条目被接受(并损坏为7个值, 必要时)。
单个crontab字段示例
支持的条目示例如下:
* */5 7/8 3-25/7 3,7,9 0-10,30-40/5
对于一周中的月或日条目,使用3个字母的月或日缩写 可用于任何可选/可使用数字的左侧。
一周中的几天:
mon-fri sun-thu/2
月份:
apr-jul mar-sep/3
安装
pip install crontab
示例使用
>>> from crontab import CronTab >>> from datetime import datetime >>> # define the crontab for 25 minutes past the hour every hour ... entry = CronTab('25 * * * *') >>> # find the delay from when this was run (around 11:13AM) ... entry.next() 720.81637899999998 >>> # find the delay from when it was last scheduled ... entry.next(datetime(2011, 7, 17, 11, 25)) 3600.0
注释
“星期几”或“月日”中最多有一个值可以不是“?' 或“*”。我们违反了这里的规范,允许“*”作为“?,在本案中 其中一个值是指定的(因为有些平台不支持 '?')。
此模块还支持方便的别名:
@yearly @annually @monthly @weekly @daily @hourly
完整crontab条目及其含义示例:
30 */2 * * * -> 30 minutes past the hour every 2 hours 15,45 23 * * * -> 11:15PM and 11:45PM every day 0 1 ? * SUN -> 1AM every Sunday 0 1 * * SUN -> 1AM every Sunday (same as above) 0 0 1 jan/2 * 2011-2013 -> midnight on January 1, 2011 and the first of every odd month until the end of 2013 24 7 L * * -> 7:24 AM on the last day of every month 24 7 * * L5 -> 7:24 AM on the last friday of every month 24 7 * * Lwed-fri -> 7:24 AM on the last wednesday, thursday, and friday of every month