Last active
July 26, 2017 02:22
-
-
Save tenfyzhong/2e5e750c370e4e0295c07e60d54a0313 to your computer and use it in GitHub Desktop.
monitoring subprocess status
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 "fmt" | |
import "os" | |
import "syscall" | |
import "time" | |
func forkExec() { | |
pa := &syscall.ProcAttr{ | |
Env: os.Environ(), | |
} | |
pid, err := syscall.ForkExec("subpro", []string{}, pa) | |
if err != nil { | |
fmt.Println(err) | |
forkExec() | |
} | |
fmt.Printf("Wait4 %d\n", pid) | |
var wstatus syscall.WaitStatus | |
rusage := &syscall.Rusage{} | |
_, err = syscall.Wait4(pid, &wstatus, syscall.WUNTRACED, rusage) | |
if err != nil { | |
fmt.Printf("Wait4 error: %v\n", err) | |
} | |
if wstatus.Exited() || wstatus.CoreDump() || wstatus.Signaled() { | |
fmt.Printf("pid %d existed\n", pid) | |
forkExec() | |
} | |
} | |
func main() { | |
fmt.Println("vim-go") | |
go forkExec() | |
for { | |
fmt.Println("parent") | |
time.Sleep(time.Second) | |
} | |
} |
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 ( | |
"fmt" | |
"math/rand" | |
"os" | |
"time" | |
) | |
func main() { | |
f, err := os.OpenFile("log", os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666) | |
if err != nil { | |
fmt.Println(err) | |
return | |
} | |
fmt.Fprintf(f, "subpro start") | |
rand.Seed(time.Now().UnixNano()) | |
for { | |
fmt.Fprintln(f, "subpro") | |
time.Sleep(time.Second) | |
r := rand.Intn(5) | |
if r == 0 { | |
break | |
} else if r == 1 { | |
os.Exit(-1) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment