Add "-n" to limit number of stats before exit
This commit is contained in:
parent
445712c5fd
commit
c2ea7d14a0
@ -4,24 +4,31 @@ import (
|
|||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/shirou/gopsutil/process"
|
"github.com/shirou/gopsutil/process"
|
||||||
|
"os"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
flagPID := flag.Int("p", 1, "Gimme your PID")
|
flagPID := flag.Int("p", 1, "PID for process to monitor")
|
||||||
|
flagNUM := flag.Int("n", -1, "Stop after n times of displaying stat")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
p, _ := process.NewProcess(int32(*flagPID))
|
p, _ := process.NewProcess(int32(*flagPID))
|
||||||
v, err := p.Times()
|
v, err := p.Times()
|
||||||
|
cnt:=0
|
||||||
|
|
||||||
for {
|
for {
|
||||||
|
if *flagNUM >= 0 && cnt >= *flagNUM {
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
fmt.Printf("%d pid: %d user: %f\t system: %f\n", time.Now().Unix(), *flagPID, v.User, v.System)
|
fmt.Printf("%d pid: %d user: %f\t system: %f\n", time.Now().Unix(), *flagPID, v.User, v.System)
|
||||||
} else {
|
} else {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
}
|
}
|
||||||
time.Sleep(time.Second)
|
time.Sleep(time.Second)
|
||||||
|
cnt++
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user