remote cmds exec tool base on grpc
new executor server
grpcServer := grpc.NewServer()
apis.RegisterExecutorServer(grpcServer, &server.Executor{})
listener, err := net.Listen("unix", socketPath)
if err != nil {
log.Fatalln(err)
}
defer listener.Close()
err = grpcServer.Serve(listener)
if err != nil {
log.Fatalln(err)
}
executor client
same interface with os/exec: Run
, Start
, Wait
, Output
, CombinedOutput
StdinPipe
, StdoutPipe
, StderrPipe
client.Command("pwd").Run()
client.Command("pwd").Output()
client.Command("pwd").CombinedOutput()
different error handle
func GetExitStatus(err error) (int, bool) {
if exiterr, ok := err.(*client.ExitError); ok {
ws := exiterr.Sys().(syscall.WaitStatus)
return ws.ExitStatus(), true
} else {
return 0, false
}
}
err := client.Command("pwd").Run()
exitStatus, ok := GetExitStatus(err)
cmd := client.Command("pwd")
err := cmd.Kill()