Last active
December 16, 2015 11:49
-
-
Save MindTwister/5430466 to your computer and use it in GitHub Desktop.
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 ( | |
"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