This is a creation in Article, where the information may have evolved or changed.
Life goes on and on go Go go!!!
Today, we share with you the task of doing timed tasks in Golang, mainly about the use of open source libraries on two GitHub.
Linux under Crontab
The crontab command is commonly used in Unix-and Unix-like operating systems to set instructions that are executed periodically. The command reads the instruction from the standard input device and stores it in a "crontab" file for later reading and execution. The word derives from the Greek language Chronos (Χρόνος), which was originally meant to be time.
Typically, crontab stored instructions are activated by the daemon, Crond often run in the background, checking every minute for scheduled jobs to be executed. This type of work is generally called cron jobs.
The crontab file contains a series of jobs and instructions sent to the cron daemon. Each user can have their own crontab file, while the operating system holds a crontab file for the entire system, which is usually stored in subdirectories under/etc or/etc, and this file can only be modified by the system administrator.
Each line of the crontab file follows a specific format, separated by a space or tab into several realms, each of which can place a single or multiple numeric values.
Command
Install Crontab:
install crontabs
Start:
start
Shut down:
stop
Restart:
service crond restart
Overload:
service crond reload
View Status:
service crond status
Editing tasks:
-e
To view tasks:
-l
Delete:
-r
format
Time-sharing day and month command to run
Operation Symbols
The method of filling in multiple values in an area:
Comma (', ') separated values, for example: "1,3,4,7,8"
The conjunctions ('-') specify the range of values, for example: "1-6", meaning "1,2,3,4,5,6"
The asterisk (' * ') represents any possible value. For example, asterisks in the "hour field" are "every one hours," and so on.
The extended version of some cron programs also supports the slash ('/') operator, which is used to indicate skipping some given number. For example, "*/3" in the Hour field equals "0,3,6,9,12,15,18,21" and so on is divisible by 3 number;
Example
Execute the command once per minute:
* * ** * yourCommand
Execution of the 2nd and 10 minutes per hour:
* ** * yourCommand
Every day from 9 o'clock in the morning to 12 in the 2nd and 10 minutes of execution:
* * * yourCommand
Every two days from 9 o'clock in the morning to 12 in the 2nd and 10 minutes of the execution:
*/2 * * yourCommand
Every Monday 9 o'clock in the morning to 12 2nd and 10 minutes of execution:
* * 1 yourCommand
Robfig/cron
Installation:
get -u github.com/robfig/cron
Application:
Execute once per minute:
package mainimport ( "log" "github.com/robfig/cron")func main() { i := 0 c := cron.New() "0 */1 * * * *" func() { i++ log.Println("execute per second", i) }) c.Start() select {}}
Note the use of select:
The function of the Golang Select, similar to the Select, poll, Epoll, is to listen to the IO operation and trigger the corresponding action when the IO operation occurs.
Every day from 9 o'clock in the morning to 12 in the 2nd and 10 minutes of execution:
package mainimport ( "fmt" "github.com/robfig/cron")func main() { "2,10 9-12 * * *"// 每天上午9点到12点的第2和第10分钟执行 c := cron.New() c.AddFunc(spec, myFunc) c.Start() select {}}func myFunc() { fmt.Println("execute")}
Jasonlvhit/gocron
Installation:
get -u github.com/jasonlvhit/gocron
Perform a task every 1 seconds, performing another task every 4 seconds:
PackageMainImport("FMT" "Time" "Github.com/jasonlvhit/gocron")funcTask () {FMT. Println ("I am runnning task.", time. Now ())}funcSuperwang () {fmt. Println ("I am runnning superwang.", time. Now ())}funcMain () {s: = Gocron. Newscheduler () s.every(1). Seconds (). Do (Task) s.every(4). Seconds (). Do (Superwang) SC: = S.start ()//Keep the channel GoTest (S, SC)//Wait<-sc//It'll happens if the channel is closed}funcTest (S *gocron. Scheduler, SCChan BOOL) {time. Sleep(8* Time. Second) S.remove (Task)//remove TaskTime. Sleep(8* Time. Second) s.clear () fmt. Println ("All task removed")Close(SC)//Close the channel}