Skip to content

Instantly share code, notes, and snippets.

@MindTwister
Last active December 16, 2015 11:49
Show Gist options
  • Save MindTwister/5430466 to your computer and use it in GitHub Desktop.
Save MindTwister/5430466 to your computer and use it in GitHub Desktop.
package main
import (
"flag"
"os"
"os/exec"
"reflect"
"time"
"log"
)
func getReflectArgs(args []string) []reflect.Value {
out := make([]reflect.Value, len(args))
for index := 0; index < len(args); index++ {
out[index] = reflect.ValueOf(args[index])
}
return out
}
var interval *int
func init() {
interval = flag.Int("interval", 2, "Interval in seconds")
}
func main() {
flag.Parse()
args := flag.Args()
if len(args) == 0 {
panic("You MUST supply a command to repeat")
}
command := reflect.ValueOf(exec.Command)
reflectArgs := getReflectArgs(args)
log.Print("Running:", args," every ", *interval, " seconds ")
for {
returns := command.Call(reflectArgs)
cmdPtr := returns[0].Interface()
cmd := cmdPtr.(*exec.Cmd)
cmd.Stdout = os.Stdout
log.Print("Running:", args)
err := cmd.Run()
if err != nil {
log.Print(err)
}
time.Sleep(time.Second * time.Duration(*interval))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment