github.com/kontera-technologies/go-supervisor/v2@v2.1.0/README.md (about) 1 # go-supervisor (V2) 2 [](https://travis-ci.com/kontera-technologies/go-supervisor) 3 [](https://codecov.io/gh/kontera-technologies/go-supervisor) 4 5 Small library for supervising child processes in `Go`, it exposes `Stdout`,`Stderr` and `Stdin` in the "Go way" using channels... 6 7 ## Example 8 `echo.sh` print stuff to stdout and stderr and quit after 5 seconds... 9 ```bash 10 #!/usr/bin/env bash 11 12 echo "STDOUT MESSAGE" 13 sleep 0.1 14 echo "STDERR MESSAGE" 1>&2 15 sleep 0.1 16 ``` 17 18 `supervisor-exapmle.go` spawn and supervise the bash program... 19 ```go 20 package main 21 22 import ( 23 "fmt" 24 "path/filepath" 25 "time" 26 27 "github.com/kontera-technologies/go-supervisor/v2" 28 ) 29 30 func main() { 31 testDir, _ := filepath.Abs("testdata") 32 events := make(chan supervisor.Event) 33 p := supervisor.NewProcess(supervisor.ProcessOptions{ 34 Name: "./example.sh", 35 Dir: testDir, 36 Id: "example", 37 EventNotifier: events, 38 OutputParser: supervisor.MakeBytesParser, 39 ErrorParser: supervisor.MakeBytesParser, 40 MaxSpawns: 4, 41 MaxSpawnAttempts: 2, 42 MaxInterruptAttempts: 3, 43 MaxTerminateAttempts: 5, 44 IdleTimeout: 10 * time.Second, 45 }) 46 47 exit := make(chan bool) 48 49 go func() { 50 for { 51 select { 52 case msg := <-p.Stdout(): 53 fmt.Printf("Received STDOUT message: %s\n", *msg) 54 case msg := <-p.Stderr(): 55 fmt.Printf("Received STDERR message: %s\n", *msg) 56 case event := <-events: 57 switch event.Code { 58 case "ProcessStart": 59 fmt.Printf("Received event: %s\n", event.Code) 60 default: 61 fmt.Printf("Received event: %s - %s\n", event.Code, event.Message) 62 } 63 case <-p.DoneNotifier(): 64 fmt.Println("Closing loop we are done...") 65 close(exit) 66 return 67 } 68 } 69 }() 70 71 if err := p.Start(); err != nil { 72 panic(fmt.Sprintf("failed to start process: %s", err)) 73 } 74 75 <-exit 76 } 77 ``` 78 79 running the program should produce this output 80 ``` 81 Received event: ProcessStart 82 Received STDOUT message: STDOUT MESSAGE 83 Received STDERR message: STDERR MESSAGE 84 Received event: ProcessDone - exit status 0 85 Received event: StoppingHeartbeatMonitoring - Stop signal received. 86 Received event: Sleep - Sleeping for 1s before respwaning instance. 87 Received event: ProcessRespawn - Trying to respawn instance. 88 Received event: ProcessStart 89 Received STDOUT message: STDOUT MESSAGE 90 Received STDERR message: STDERR MESSAGE 91 Received event: ProcessDone - exit status 0 92 Received event: StoppingHeartbeatMonitoring - Stop signal received. 93 Received event: Sleep - Sleeping for 1s before respwaning instance. 94 Received event: ProcessRespawn - Trying to respawn instance. 95 Received event: ProcessStart 96 Received STDOUT message: STDOUT MESSAGE 97 Received STDERR message: STDERR MESSAGE 98 Received event: ProcessDone - exit status 0 99 Received event: StoppingHeartbeatMonitoring - Stop signal received. 100 Received event: Sleep - Sleeping for 1s before respwaning instance. 101 Received event: ProcessRespawn - Trying to respawn instance. 102 Received event: ProcessStart 103 Received STDOUT message: STDOUT MESSAGE 104 Received STDERR message: STDERR MESSAGE 105 Received event: ProcessDone - exit status 0 106 Received event: StoppingHeartbeatMonitoring - Stop signal received. 107 Received event: RespawnError - Max number of respawns reached. 108 Closing loop we are done... 109 ```