Skip to content

Instantly share code, notes, and snippets.

@tyndyll
Last active August 29, 2015 14:02
Show Gist options
  • Save tyndyll/b55a723958fc7224a41d to your computer and use it in GitHub Desktop.
Save tyndyll/b55a723958fc7224a41d to your computer and use it in GitHub Desktop.
Simple script/binary that prints a message at a prescribed --interval either infinitely or for --count number of times.
// author: tyndyll
// Simple script/binary that prints a message at a prescribed --interval either infinitely or
// for --count number of times.
// It is being used a simple service target for setting up and testing systemd files. To that
// end it handles SIGINT
//
// To use:
//
// ~GOPATH go run src/cheerleader.go --interval 1 --count 1
// I am the cheerleader, I will save the world
// Exiting
// ~GOPATH go install src/cheerleader.go
// ~GOPATH bin/cheerleader
// I am the cheerleader, I will save the world
// ^Csignal = interrupt
// SIGINT caught. Exiting
package main
import (
"flag"
"fmt"
"os"
"os/signal"
"syscall"
"time"
)
func main() {
message := flag.String("message", "I am the cheerleader, I will save the world", "Message to be printed")
interval := flag.Int("interval", 5, "Number of seconds between iterations")
count := flag.Int("count", 0, "Number of iterations (<1 for forever)")
flag.Parse()
// Handle SIGINT
sigchan := make(chan os.Signal, 1)
signal.Notify(sigchan, syscall.SIGINT)
go func() {
select {
case sig := <-sigchan:
fmt.Println("signal = ", sig)
if sig == os.Interrupt {
fmt.Println("SIGINT caught. Exiting")
os.Exit(0)
}
}
}()
loop_forever := false
if *count < 1 {
loop_forever = true
}
for {
fmt.Println(*message)
time.Sleep(time.Second * time.Duration(*interval))
if !loop_forever {
*count--
if *count < 1 {
break
}
}
}
fmt.Println("Exiting")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment