Golang os.StarProcess(), os.Process.Kill(), Wait(), Signal() and Release() functions example

package os

Golang os.StarProcess(), os.Process.Kill(), Wait(), Signal() and Release() functions usage example

 package main

 import (
 "fmt"
 "os"
 "syscall"
 )

 func main() {
 r, w, err := os.Pipe()

 if err != nil {
 panic(err)
 }

 defer r.Close()

 process, err := os.StartProcess("/bin/ps", []string{"-ef"}, &os.ProcAttr{Files: []*os.File{nil, w, os.Stderr}})

 if err != nil {
 panic(err)
 }

 processState, err := process.Wait()

 if err != nil {
 panic(err)
 }

 err = process.Release()

 if err != nil {
 panic(err)
 }

 fmt.Println("Did the child process exited? : ", processState.Exited())
 fmt.Println("So the child pid is? : ", processState.Pid())
 fmt.Println("Exited successfully? : ", processState.Success())

 fmt.Println("Exited system CPU time ? : ", processState.SystemTime())
 fmt.Println("Exited user CPU time ? : ", processState.UserTime())

 // just to be sure, let's kill again
 err = process.Signal(syscall.SIGKILL)

 if err != nil {
 fmt.Println(err) // see what the serial killer has to say
 return
 }

 w.Close()

 }

Sample output :

Did the child process exited? : true

So the child pid is? : 1376

Exited successfully? : true

Exited system CPU time ? : 3.722ms

Exited user CPU time ? : 984µs

os: process already released

References :

http://golang.org/pkg/os/#StartProcess

http://golang.org/pkg/os/#Process.Kill

http://golang.org/pkg/os/#Process.Release

http://golang.org/pkg/os/#Process.Signal

Advertisement