-
-
Save rubikill/215d218465ae734b9eeb5c716ce875c1 to your computer and use it in GitHub Desktop.
Golang - Keep more than 1 program running forever (similar to cron job but shorter interval)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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