scratchpad: repeat: run a command in a certain interval or as soon as it finishes

This commit is contained in:
Christian Schwarz 2017-07-07 14:23:53 +02:00
parent 2c13fbe6ec
commit 8e378d76b9
2 changed files with 52 additions and 0 deletions

46
scratchpad/repeat/main.go Normal file
View File

@ -0,0 +1,46 @@
package main
import (
"log"
"os"
"os/exec"
"time"
)
var config struct {
duration time.Duration
}
func usage() {
log.Printf("usage: repeat repeatInterval command [args...]")
}
func main() {
args := os.Args
if len(args) < 3 {
usage()
os.Exit(1)
}
repeatInterval, err := time.ParseDuration(args[1])
if err != nil {
log.Printf("cannot parse interval: %s", err)
usage()
os.Exit(1)
}
var lastStart time.Time
for {
cmd := exec.Command(args[2], args[3:]...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
lastStart = time.Now()
if err := cmd.Run(); err != nil {
panic(err)
}
time.Sleep(lastStart.Add(repeatInterval).Sub(time.Now()))
}
}

6
scratchpad/repeat/sleepx.sh Executable file
View File

@ -0,0 +1,6 @@
#!/bin/sh
echo -n ' start '
date
sleep $1
echo -n 'done '
date