add command: retry;

develop
Ge Song 2 years ago
parent bd1fc116ec
commit 170bdd0b45

@ -0,0 +1,3 @@
在程序返回不为0时重试
Usage: `retry -c 10 -i 2s -- <command> <args>`

@ -0,0 +1,3 @@
module main
go 1.20

@ -0,0 +1,48 @@
package main
import (
"flag"
"fmt"
"os"
"os/exec"
"time"
)
var (
count int
interval time.Duration
)
func main() {
flag.IntVar(&count, "c", 5, "maximum execution times")
flag.DurationVar(&interval, "i", time.Second, "retry interval")
flag.Parse()
if count < 1 {
count = 1
}
if interval < 0 {
interval = 0
}
args := flag.Args()
if len(args) == 0 {
fmt.Println("Please give the command to execute!")
os.Exit(1)
}
var err error
for i := 0; i < count; i++ {
if i > 0 {
fmt.Printf("retry %d after %s...\n", i, interval)
time.Sleep(interval)
}
cmd := exec.Command(args[0], args[1:]...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
if err == nil {
break
}
}
if err != nil {
os.Exit(1)
}
}
Loading…
Cancel
Save