Created
February 13, 2017 21:30
-
-
Save rebeccaskinner/dca175686b898418f48c91ddd0640f28 to your computer and use it in GitHub Desktop.
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" | |
"io/ioutil" | |
"os" | |
"os/exec" | |
"strings" | |
"time" | |
) | |
func main() { | |
ch := make(chan int) | |
done := make(chan bool) | |
go func() { | |
var ( | |
name string | |
args []string | |
) | |
name = os.Args[1] | |
if len(os.Args) > 2 { | |
args = os.Args[2:] | |
} | |
cmd := exec.Command(name, args...) | |
if err := cmd.Start(); err != nil { | |
fmt.Println(err) | |
ch <- -1 | |
} | |
ch <- cmd.Process.Pid | |
cmd.Wait() | |
done <- true | |
}() | |
pid := <-ch | |
if -1 == pid { | |
return | |
} | |
go func() { | |
fmt.Println("Monitoring process: ", pid) | |
path := fmt.Sprintf("/proc/%d/status", pid) | |
for { | |
status, err := ioutil.ReadFile(path) | |
if err != nil { | |
return | |
} | |
results := strings.Split(string(status), "\n") | |
fmt.Println(time.Now()) | |
fmt.Println(results[15]) | |
fmt.Println(results[16]) | |
fmt.Println("--------") | |
time.Sleep(1 * time.Second) | |
} | |
}() | |
<-done | |
return | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment