Skip to content

Instantly share code, notes, and snippets.

@LeMeteore
Forked from stupidbodo/cron.go
Created January 20, 2016 15:53
Show Gist options
  • Save LeMeteore/c7579b881774f52f12a8 to your computer and use it in GitHub Desktop.
Save LeMeteore/c7579b881774f52f12a8 to your computer and use it in GitHub Desktop.
Golang - Keep more than 1 program running forever (similar to cron job but shorter interval)
package main
import (
"fmt"
"runtime"
"time"
)
var (
INTERVAL_SEC = 10
)
func PrintRoutine1(intervalInSec int) {
t := time.NewTicker(time.Duration(intervalInSec) * time.Second)
for _ = range t.C {
fmt.Println("PrintRoutine1")
}
}
func PrintRoutine2(intervalInSec int) {
t := time.NewTicker(time.Duration(intervalInSec) * time.Second)
for _ = range t.C {
fmt.Println("PrintRoutine2")
}
}
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
go PrintRoutine1(INTERVAL_SEC)
go PrintRoutine2(INTERVAL_SEC)
// block forever so that your program won't end
select {}
}
// PrintRoutine1
// PrintRoutine2
// PrintRoutine1
// PrintRoutine2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment