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
Advertisement
Something interesting
Tutorials
+11.5k Golang : Change date format to yyyy-mm-dd
+9.5k Golang : Changing a RGBA image number of channels with OpenCV
+7.9k Golang : Grayscale Image
+19k Golang : Padding data for encryption and un-padding data for decryption
+7.3k Golang : How to fix html/template : "somefile" is undefined error?
+15k Golang : How do I get the local IP (non-loopback) address ?
+29.5k Golang : Saving(serializing) and reading file with GOB
+6.7k Golang : Check if password length meet the requirement
+15.2k Golang : How to check if IP address is in range
+40.1k Golang : UDP client server read write example
+9.7k Golang : Find correlation coefficient example