2019-05-06 16:11:38 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"github.com/shirou/gopsutil/process"
|
2019-05-07 14:49:20 +02:00
|
|
|
"os"
|
2019-05-06 16:11:38 +02:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
2019-05-07 14:49:20 +02:00
|
|
|
flagPID := flag.Int("p", 1, "PID for process to monitor")
|
|
|
|
flagNUM := flag.Int("n", -1, "Stop after n times of displaying stat")
|
2019-05-06 16:11:38 +02:00
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
p, _ := process.NewProcess(int32(*flagPID))
|
|
|
|
v, err := p.Times()
|
2019-05-07 14:49:20 +02:00
|
|
|
cnt:=0
|
2019-05-06 16:11:38 +02:00
|
|
|
|
|
|
|
for {
|
2019-05-07 14:49:20 +02:00
|
|
|
if *flagNUM >= 0 && cnt >= *flagNUM {
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
2019-05-06 16:11:38 +02:00
|
|
|
if err == nil {
|
|
|
|
fmt.Printf("%d pid: %d user: %f\t system: %f\n", time.Now().Unix(), *flagPID, v.User, v.System)
|
|
|
|
} else {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
time.Sleep(time.Second)
|
2019-05-07 14:49:20 +02:00
|
|
|
cnt++
|
2019-05-06 16:11:38 +02:00
|
|
|
}
|
|
|
|
}
|