top

This library implements a cron spec parser and runner. See the README for more details. Package cron implements a cron spec parser and job runner.

Usage

Callers may register Funcs to be invoked on a given schedule. Cron will run them in their own goroutines.

c := cron.New()
c.AddFunc("0 30 * * * *", func() { fmt.Println("Every hour on the half hour") })
c.AddFunc("@hourly",      func() { fmt.Println("Every hour") })
c.AddFunc("@every 1h30m", func() { fmt.Println("Every hour thirty") })
c.Start()
..
// Funcs are invoked in their own goroutine, asynchronously.
...
// Funcs may also be added to a running Cron
c.AddFunc("@daily", func() { fmt.Println("Every day") })
..
// Inspect the cron job entries' next and previous run times.
inspect(c.Entries())
..
c.Stop()  // Stop the scheduler (does not stop any jobs already running).

CRON Expression Format

A cron expression represents a set of times, using 6 space-separated fields.

Field name   | Mandatory? | Allowed values  | Allowed special characters
----------   | ---------- | --------------  | --------------------------
Seconds      | Yes        | 0-59            | * / , -
Minutes      | Yes        | 0-59            | * / , -
Hours        | Yes        | 0-23            | * / , -
Day of month | Yes        | 1-31            | * / , - ?
Month        | Yes        | 1-12 or JAN-DEC | * / , -
Day of week  | Yes        | 0-6 or SUN-SAT  | * / , - ?

Note: Month and Day-of-week field values are case insensitive. "SUN", "Sun", and "sun" are equally accepted.

Special Characters

Asterisk ( * )

The asterisk indicates that the cron expression will match for all values of the field; e.g., using an asterisk in the 5th field (month) would indicate every month.

Slash ( / )

Slashes are used to describe increments of ranges. For example 3-59/15 in the 1st field (minutes) would indicate the 3rd minute of the hour and every 15 minutes thereafter. The form "*\/..." is equivalent to the form "first-last/...", that is, an increment over the largest possible range of the field. The form "N/..." is accepted as meaning "N-MAX/...", that is, starting at N, use the increment until the end of that specific range. It does not wrap around.

Comma ( , )

Commas are used to separate items of a list. For example, using "MON,WED,FRI" in the 5th field (day of week) would mean Mondays, Wednesdays and Fridays.

Hyphen ( - )

Hyphens are used to define ranges. For example, 9-17 would indicate every hour between 9am and 5pm inclusive.

Question mark ( ? )

Question mark may be used instead of '*' for leaving either day-of-month or day-of-week blank.

Predefined schedules

You may use one of several pre-defined schedules in place of a cron expression.

Entry                  | Description                                | Equivalent To
-----                  | -----------                                | -------------
@yearly (or @annually) | Run once a year, midnight, Jan. 1st        | 0 0 0 1 1 *
@monthly               | Run once a month, midnight, first of month | 0 0 0 1 * *
@weekly                | Run once a week, midnight on Sunday        | 0 0 0 * * 0
@daily (or @midnight)  | Run once a day, midnight                   | 0 0 0 * * *
@hourly                | Run once an hour, beginning of hour        | 0 0 * * * *

Intervals

You may also schedule a job to execute at fixed intervals. This is supported by formatting the cron spec like this:

@every <duration>

where "duration" is a string accepted by time.ParseDuration (http://golang.org/pkg/time/#ParseDuration).

For example, "@every 1h30m10s" would indicate a schedule that activates every 1 hour, 30 minutes, 10 seconds.

Note: The interval does not take the job runtime into account. For example, if a job takes 3 minutes to run, and it is scheduled to run every 5 minutes, it will have only 2 minutes of idle time between each run.

Time zones

All interpretation and scheduling is done in the machine's local time zone (as provided by the Go time package (http://www.golang.org/pkg/time).

Be aware that jobs scheduled during daylight-savings leap-ahead transitions will not be run!

Thread safety

Since the Cron service runs concurrently with the calling code, some amount of care must be taken to ensure proper synchronization.

All cron methods are designed to be correctly synchronized as long as the caller ensures that invocations have a clear happens-before ordering between them.

Implementation

Cron entries are stored in an array, sorted by their next activation time. Cron sleeps until the next job is due to be run.

Upon waking:

- it runs each entry that is active on that second
- it calculates the next run times for the jobs that were run
- it re-sorts the array of entries by next activation time.
- it goes to sleep until the soonest job.

Imported by 331 package(s) ΒΆ

  1. github.com/18F/angrytock
  2. github.com/18F/cf-go-cron
  3. github.com/7ing/kubernetes/pkg/controller/scheduledjob
  4. github.com/8miricd/DempaUOCalendarClone
  5. github.com/ActiveState/dockron
  6. github.com/Activestate/dockron
  7. github.com/ArturoVM/pond
  8. github.com/AustinDizzy/harborly-watch
  9. github.com/Bjorn248/ec2-stopwatch
  10. github.com/Bruce313/timer
  11. github.com/Clever/cron-admin/server
  12. github.com/Collinux/snap/pkg/schedule
  13. github.com/CrunchyData/crunchy-containers/dba
  14. github.com/CrunchyData/crunchy-postgresql-manager-openshift/backup
  15. github.com/CrunchyData/crunchy-postgresql-manager/task
  16. github.com/Daemoneyes/BabelProxy/PlatformProvider
  17. github.com/DispatchMe/go-work
  18. github.com/FederationOfFathers/dashboard/store
  19. github.com/GPMGo/switch/models
  20. github.com/Gooooodman/src/base
  21. github.com/Gooooodman/src/crond
  22. github.com/IRCody/snap/pkg/schedule
  23. github.com/Imvoo/Displosu
  24. github.com/Integralist/ufc-event-notifier
  25. github.com/Julianzz/gogs/modules/cron
  26. github.com/Jumpscale/aysfs
  27. github.com/ListHub/please/cron
  28. github.com/ListHub/please/model
  29. github.com/Luzifer/dockermanager
  30. github.com/Luzifer/dockermanager/config
  31. github.com/Luzifer/dockerproxy
  32. github.com/Luzifer/ediplug_ctrl
  33. github.com/Luzifer/gobuilder
  34. github.com/Luzifer/habitscheduler
  35. github.com/PaperCutSoftware/silver/service
  36. github.com/PoolC/slack_bot/bot
  37. github.com/SkygearIO/skygear-server
  38. github.com/Sn0rt/kubernetes/pkg/controller/scheduledjob
  39. github.com/Swind/gogs/modules/cron
  40. github.com/Syfaro/bot
  41. github.com/Unknwon/gowalker/models
  42. github.com/WindomZ/go-cron
  43. github.com/Xe/Tetra/bot
  44. github.com/a-palchikov/kubernetes/pkg/controller/scheduledjob
  45. github.com/a16/fake-rtrd
  46. github.com/abiosoft/caddy-hugo/editor
  47. github.com/acierto/news-scraper
  48. github.com/activestate/dockron
  49. github.com/ajvb/pond
  50. github.com/almighty/almighty-core/remoteworkitem
  51. github.com/alyakimov/pixel
  52. github.com/alyakimov/pixel-carusel
  53. github.com/amrhassan/agentcontroller2-scrubbed
  54. github.com/anarcher/go-cron
  55. github.com/andrew-d/site-monitor
  56. github.com/andyxning/kubernetes/pkg/controller/cronjob
  57. github.com/anxiousmodernman/caddy-hugo
  58. github.com/aoisensi/akasoryo
  59. github.com/aprilsky/gogsweb/models
  60. github.com/aranair/go-kiasu
  61. github.com/areed/kubernetes/pkg/apis/batch/validation
  62. github.com/areed/kubernetes/pkg/controller/scheduledjob
  63. github.com/arjandepooter/1337bot
  64. github.com/aspic/bot
  65. github.com/austindizzy/harborly-watch
  66. github.com/baijum/almighty-core/remoteworkitem
  67. github.com/baijum/gogs/modules/cron
  68. github.com/benlightning/ms2mysql
  69. github.com/benspotatoes/luminous-monk
  70. github.com/betacraft/lemonade
  71. github.com/better0332/kubernetes/pkg/controller/scheduledjob
  72. github.com/bevly/bevly/syncschedule
  73. github.com/binlaniua/kitgo/file
  74. github.com/briandowns/aion/dispatcher
  75. github.com/briandowns/raceway/scheduler
  76. github.com/browny/goweb-scaffold/cron
  77. github.com/brunoqc/gogs/modules/cron
  78. github.com/bushwood/metre
  79. github.com/casualjim/exeggutor
  80. github.com/casualjim/exeggutor/agora
  81. github.com/cheyang/kubernetes/pkg/controller/scheduledjob
  82. github.com/chop-dbhi/dcc
  83. github.com/christiangalsterer/execbeat/beat
  84. github.com/christiangalsterer/httpbeat/beater
  85. github.com/cloudwan/gohan/server
  86. github.com/codingneo/tweetsbot
  87. github.com/coffeehc/silver/service
  88. github.com/collinux/snap/pkg/schedule
  89. github.com/contactless/wb-rules/wbrules
  90. github.com/coralproject/pillar/app/pillar/route
  91. github.com/core433/restaurant-go
  92. github.com/corvramirez/go-cron-jobs
  93. github.com/coseyo/getsale/controllers
  94. github.com/crunchydata/crunchy-containers/dba
  95. github.com/crunchydata/crunchy-postgresql-manager-openshift/backup
  96. github.com/crunchydata/crunchy-postgresql-manager/task
  97. github.com/csrwng/kubernetes/pkg/controller/scheduledjob
  98. github.com/danielscottt/snap/pkg/schedule
  99. github.com/darciopacifico/taskmanager/taskscheduler
  100. github.com/darwin/scraper
  101. github.com/ddrozdov/httpbeat/beat
  102. github.com/deckarep/metre
  103. github.com/deftlabs/dlshared
  104. github.com/derekparker/kubernetes/pkg/controller/scheduledjob
  105. github.com/dustinblackman/pokepush
  106. github.com/dustinrc/deis/mesos/pkg/boot
  107. github.com/econnell/deis/mesos/pkg/boot
  108. github.com/eedevops/ilm-statistics
  109. github.com/eirka/eirka-admin/utils
  110. github.com/elcct/kubernetes/pkg/controller/cronjob
  111. github.com/elct9620/go-plurk-robot/robot
  112. github.com/ericchiang/kubernetes/pkg/controller/scheduledjob
  113. github.com/exu/go-workshops/665-libs-cron
  114. github.com/fabiorphp/snapshooter
  115. github.com/farhaven/glenda
  116. github.com/figoxu/Figo
  117. github.com/figoxu/goPraticse/cronTask
  118. github.com/foomo/crontinuous
  119. github.com/freeusd/solebtc
  120. github.com/fsamin/intools-engine/common/server
  121. github.com/fsamin/intools-engine/common/tests
  122. github.com/fsamin/intools-engine/intools
  123. github.com/fsr/matthias/plugins/bday
  124. github.com/fsr/matthias/plugins/mensa
  125. github.com/g8os/controller/scheduling
  126. github.com/g8os/fs
  127. github.com/g8os/fs/watcher
  128. github.com/gemsi/godoit
  129. github.com/georgethomas111/metre
  130. github.com/getblank/blank-cron
  131. github.com/gitchander/go-examples/schedulers/cron
  132. github.com/go-chat-bot/bot
  133. github.com/go-gitea/website/models
  134. github.com/gocraft/work
  135. github.com/gogits/gogsweb/models
  136. github.com/gophergala2016/skeddy
  137. github.com/gospackler/metre
  138. github.com/gpmgo/switch/models
  139. github.com/hacdias/caddy-hugo
  140. github.com/hefju/gogsweb/models
  141. github.com/helphone/importer
  142. github.com/henyouqian/SldServer/match
  143. github.com/henyouqian/lvdb/server
  144. github.com/hhsnopek/10kft
  145. github.com/hhsnopek/pill
  146. github.com/hilerchyn/gogs/modules/cron
  147. github.com/iMax-pp/intools-engine/intools
  148. github.com/iamthemuffinman/kubernetes/pkg/apis/batch/validation
  149. github.com/intelsdi-x/kubernetes/pkg/controller/cronjob
  150. github.com/intelsdi-x/snap/pkg/schedule
  151. github.com/intervention-engine/ie
  152. github.com/intervention-engine/integrator
  153. github.com/intervention-engine/multifactorriskservice
  154. github.com/intervention-engine/multifactorriskservice/server
  155. github.com/ivanfoo/ofelia/core
  156. github.com/iwankgb/snap/pkg/schedule
  157. github.com/izacus/PrometPush
  158. github.com/j-musca/mutservice
  159. github.com/jackfrancis/deis/mesos/pkg/boot
  160. github.com/jakecoffman/send-psalms
  161. github.com/jalkanen/work
  162. github.com/jawr/dns/watcher
  163. github.com/jaytaylor/shipbuilder/src
  164. github.com/jbking/gohan/server
  165. github.com/jchorl/MunchBot/server
  166. github.com/jchorl/munchbot/server
  167. github.com/jerry1022/ssdb2hbase
  168. github.com/jessemillar/stalks
  169. github.com/jfrazelle/kubernetes/pkg/controller/cronjob
  170. github.com/jkhelil/kubernetes/pkg/controller/scheduledjob
  171. github.com/jniltinho/gogsweb/models
  172. github.com/joakim666/ismonitor
  173. github.com/joakim666/pjek-monitor
  174. github.com/joh-m/amz-viz
  175. github.com/johnhof/metre
  176. github.com/jonpchin/GoChess/gostuff
  177. github.com/jsix/go2o/app/daemon
  178. github.com/juju2013/gogs/modules/cron
  179. github.com/jvikstedt/alarmii_old/scheduler
  180. github.com/karolhor/go-workshops/665-libs-cron
  181. github.com/kevinxu001/gogsweb/models
  182. github.com/kildevaeld/gcron
  183. github.com/kr9ly/taskar
  184. github.com/kr9ly/taskar/main
  185. github.com/kubernetes/kubernetes/pkg/apis/batch/validation
  186. github.com/kyokomi/slackbot/plugins/cron
  187. github.com/l-lin/mr-tracker-api/feed
  188. github.com/l-lin/wn-tracker-api/feed
  189. github.com/linlexing/dbgo
  190. github.com/linquize/gogsweb/models
  191. github.com/listhub/please/cron
  192. github.com/listhub/please/model
  193. github.com/lovek323/btstat
  194. github.com/lyanchih/FreewayCrawler
  195. github.com/lynxbat/snap/pkg/schedule
  196. github.com/maddyonline/bot
  197. github.com/makerbot/go-cron
  198. github.com/makhov/gogsweb/models
  199. github.com/malix0/gogs/modules/cron
  200. github.com/mamayu/deis/mesos/pkg/boot
  201. github.com/marcolenzo/ipwatchdog
  202. github.com/martinusso/bot
  203. github.com/mattyr/flynn-pgbackups
  204. github.com/maxwellhealth/goproc
  205. github.com/mcuadros/ofelia/core
  206. github.com/michaloo/go-cron
  207. github.com/michelleN/deis/mesos/pkg/boot
  208. github.com/microcosm-cc/gosnippet/server
  209. github.com/microcosm-cc/microcosm/server
  210. github.com/milescrabill/reaper/reaper
  211. github.com/millken/ydaemon
  212. github.com/mischief/glenda
  213. github.com/mopsalarm/go-pr0gramm-meta-update
  214. github.com/mopsalarm/pr0gramm-tags
  215. github.com/mozilla-services/reaper/reaper
  216. github.com/mukku0627/go-bot
  217. github.com/myafeier/go2o/app/daemon
  218. github.com/n3r0-ch/vaultsha.re/cronjob
  219. github.com/nati/gohan/server
  220. github.com/neverlock/PacktpubFreeAlert
  221. github.com/ngauthier/deis/mesos/pkg/boot
  222. github.com/nubunto/fixr/cmd
  223. github.com/nubunto/vise/destroyer
  224. github.com/odise/go-cron
  225. github.com/oiooj/Agent/CronDaemon
  226. github.com/olomix/snap/pkg/schedule
  227. github.com/oracleJava/modules/jobs/app/controllers
  228. github.com/oracleJava/modules/jobs/app/jobs
  229. github.com/orian/utils/concurency
  230. github.com/ota42y/chatwork-command/command
  231. github.com/ota42y/my_go_post_data_crawler
  232. github.com/papercutsoftware/silver/service
  233. github.com/phonkee/pond
  234. github.com/pjh130/go/common/cron
  235. github.com/plutov/kubernetes/pkg/controller/scheduledjob
  236. github.com/polor1010/freewayanalysis
  237. github.com/portworx/kubernetes/pkg/controller/scheduledjob
  238. github.com/ppalucki/snap/pkg/schedule
  239. github.com/pzduniak/aiw3-golang-3k/backend
  240. github.com/qgweb/new/filedb
  241. github.com/qjpcpu/logger
  242. github.com/rach/poda
  243. github.com/rach/pome
  244. github.com/randall2602/snap/pkg/schedule
  245. github.com/random-liu/kubernetes/pkg/controller/scheduledjob
  246. github.com/raphael/go-cron
  247. github.com/ravitezu/deis/mesos/pkg/boot
  248. github.com/rchargel/localiday/db
  249. github.com/remigijusj/clubman/src
  250. github.com/rightscale/croner/cron
  251. github.com/robfig/revel/modules/jobs/app/controllers
  252. github.com/robfig/revel/modules/jobs/app/jobs
  253. github.com/robxu9/kahinah
  254. github.com/rogierlommers/slack-server/internal/tasks
  255. github.com/rvaralda/deis/mesos/pkg/boot
  256. github.com/safawo/webcron
  257. github.com/saj1th/reaper/reaper
  258. github.com/sbuss/deis/mesos/pkg/boot
  259. github.com/sebest/hooky/models
  260. github.com/sent-hil/learn/go
  261. github.com/sescobb27/ciudad-gourmet/services/log
  262. github.com/sethjback/gobl/coordinator/manager
  263. github.com/sgoings/deis/mesos/pkg/boot
  264. github.com/shashankmjain/deis/mesos/pkg/boot
  265. github.com/skygeario/skygear-server
  266. github.com/skygeario/skygear-server/pkg/server/plugin
  267. github.com/slok/khronos/schedule
  268. github.com/slok/khronos/service/validate
  269. github.com/smothiki/deis/mesos/pkg/boot
  270. github.com/soh335/radicast
  271. github.com/solefaucet/sole-server
  272. github.com/srhopkins/aion/dispatcher
  273. github.com/ssr66994053/ant
  274. github.com/stepanstipl/kubernetes/pkg/controller/scheduledjob
  275. github.com/studygolang/studygolang/src/server/crawler
  276. github.com/studygolang/studygolang/src/server/indexer
  277. github.com/studygolang/studygolang/src/server/studygolang
  278. github.com/sugeladi/goWeb/cron
  279. github.com/syfaro/bot
  280. github.com/szank/switch/models
  281. github.com/taoh/gocelery
  282. github.com/techjanitor/pram-admin/utils
  283. github.com/technosophos/deis/mesos/pkg/boot
  284. github.com/thockin/kubernetes/pkg/controller/cronjob
  285. github.com/threatstream/shipbuilder/src
  286. github.com/timjwright/godoit
  287. github.com/toophy/go2o/app/daemon
  288. github.com/toophy/gowalker/models
  289. github.com/treeherder/glenda
  290. github.com/ts33kr/boot
  291. github.com/tschottdorf/kubernetes/pkg/apis/batch/validation
  292. github.com/tsileo/blobsnap/scheduler
  293. github.com/tzjin/sniksnak/system
  294. github.com/unknwon/gowalker/models
  295. github.com/upmc-enterprises/kubernetes/pkg/controller/scheduledjob
  296. github.com/uppfinnarn/gubal/surveyor
  297. github.com/uswitch/dagr/schedule
  298. github.com/uwork/gorond/goron
  299. github.com/vangdfang/gerrit-ci
  300. github.com/vincent3i/beego-blog/task
  301. github.com/vsayer/pome
  302. github.com/wanzhihu/zhihuSpider
  303. github.com/weichaoduo/zeromore/global
  304. github.com/weichaoduo/zeromore/hub
  305. github.com/weichaoduo/zeromore/worker
  306. github.com/weidewang/aws-man/rig-agent
  307. github.com/weidewang/gogs/modules/cron
  308. github.com/welcome-to-shire/gron
  309. github.com/wenbo/gogs/modules/cron
  310. github.com/winkapp/deis/mesos/pkg/boot
  311. github.com/woodsaj/snap/pkg/schedule
  312. github.com/xchapter7x/chaospeddler/service_broker
  313. github.com/xuender/family/clan
  314. github.com/yanzay/autohome
  315. github.com/ylqjgm/SCBlog
  316. github.com/yokoi-h/gohan/server
  317. github.com/yuin/golbot
  318. github.com/zaybiz/go-cron
  319. github.com/zdebeer99/zhome/pkg/stateengine
  320. github.com/zet4/catsbutnotreally
  321. github.com/zier/work
  322. github.com/zlisthq/zlist
  323. github.com/zwh8800/cloudxns-ddns
  324. github.com/zwh8800/golang-mirror
  325. github.com/zwh8800/md-blog-gen/crontab
  326. github.com/zzhua/gogsweb/models
  327. gopkg.in/kyokomi/slackbot.v1/plugins/cron
  328. gopkg.in/kyokomi/slackbot.v2/plugins/cron
  329. gopkg.in/kyokomi/slackbot.v3/plugins/cron
  330. k8s.io/kubernetes/pkg/apis/batch/validation
  331. k8s.io/kubernetes/pkg/controller/cronjob