This is a creation in Article, where the information may have evolved or changed.
Linux cron allows the system to perform a specified task at a specified time, and crontab is used to manage the cron mechanism. On the server side, cron can be used to perform certain tasks periodically, such as regular database backups, periodically compressing server-side data, and so on.
Now with the Golang server is growing, the server needs to periodically backup or compress or delete data is the norm. This week to learn the Golang version of the crontab, feel that the function is very convenient.
1 Package Main2 3 Import (4 "FMT"5 "Github.com/robfig/cron"6 )7 8 Func Main () {9Spec: ="0, 2 , *, *, *" //every morning .Tenc: =Cron. New () One C.addfunc (Spec, callyourfunc) A C.start () - Select {} - } the - func Callyourfunc () { -Fmt. Println ("Callyourfunc come here.") - } + - //Callyourfunc function is called every 2:30
Although this program is short, it has been implemented periodically to perform specific tasks. For the specified time, it is generally recorded in the configuration file, and then through the Golang parsing configuration file to implement the code part without modification, and only need to modify the configuration file can easily change the specified time.
Reference Document: http://ju.outofmemory.cn/entry/65356