mirror of
https://github.com/rancher/os.git
synced 2025-09-02 15:24:32 +00:00
Add respawn command
This commit is contained in:
2
main.go
2
main.go
@@ -6,6 +6,7 @@ import (
|
||||
log "github.com/Sirupsen/logrus"
|
||||
"github.com/docker/docker/pkg/reexec"
|
||||
osInit "github.com/rancherio/os/init"
|
||||
"github.com/rancherio/os/respawn"
|
||||
"github.com/rancherio/os/sysinit"
|
||||
"github.com/rancherio/os/user"
|
||||
)
|
||||
@@ -17,6 +18,7 @@ func main() {
|
||||
reexec.Register("/sbin/init-sys", sysinit.SysInit)
|
||||
reexec.Register("/usr/bin/system-docker", user.SystemDocker)
|
||||
reexec.Register("system-docker", user.SystemDocker)
|
||||
reexec.Register("respawn", respawn.Main)
|
||||
|
||||
if !reexec.Init() {
|
||||
log.Fatalf("Failed to find an entry point for %s", os.Args[0])
|
||||
|
65
respawn/respawn.go
Normal file
65
respawn/respawn.go
Normal file
@@ -0,0 +1,65 @@
|
||||
package respawn
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
log "github.com/Sirupsen/logrus"
|
||||
)
|
||||
|
||||
func Main() {
|
||||
input, err := ioutil.ReadAll(os.Stdin)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
var wait sync.WaitGroup
|
||||
|
||||
for _, line := range strings.Split(string(input), "\n") {
|
||||
wait.Add(1)
|
||||
go execute(line, wait)
|
||||
}
|
||||
|
||||
wait.Wait()
|
||||
}
|
||||
|
||||
func execute(line string, wait sync.WaitGroup) {
|
||||
start := time.Now()
|
||||
count := 0
|
||||
|
||||
for {
|
||||
args := strings.Split(line, " ")
|
||||
|
||||
cmd := exec.Command(args[0], args[1:]...)
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
|
||||
err := cmd.Start()
|
||||
if err != nil {
|
||||
log.Error("%s : %v", line, err)
|
||||
}
|
||||
|
||||
err = cmd.Wait()
|
||||
if err != nil {
|
||||
log.Error("%s : %v", line, err)
|
||||
}
|
||||
|
||||
count++
|
||||
|
||||
if count > 10 {
|
||||
if start.Sub(time.Now()) <= (1 * time.Second) {
|
||||
log.Errorf("%s : restarted too fast, not executing", line)
|
||||
break
|
||||
}
|
||||
|
||||
count = 0
|
||||
start = time.Now()
|
||||
}
|
||||
}
|
||||
|
||||
wait.Done()
|
||||
}
|
Reference in New Issue
Block a user