Golang 进程管理工具: gopsutil

gopsutil 是用go 实现python 写的psutil 的功能,并尝试在多平台上完整psutil 的函数。

下面是对内存的显示:

package main

import (
	"fmt"
	"github.com/shirou/gopsutil/mem"
)

func main(){
	v, _ := mem.VirtualMemory()

	// almost every return value is a struct
	fmt.Printf("Total: %v, Free:%v, UsedPercent:%f%%\n", v.Total, v.Free, v.UsedPercent)

	// convert to JSON. String() is also implemented
	fmt.Println(v)
}

在我的电脑上输出:

{"total":17179869184,"available":9234153472,"used":7945715712,"usedPercent":46.250152587890625,"free":5201149952,"active":3655434240,"inactive":4033003520,"wired":2986549248,"laundry":0,"buffers":0,"cached":0,"writeback":0,"dirty":0,"writebacktmp":0,"shared":0,"slab":0,"pagetables":0,"swapcached":0,"commitlimit":0,"committedas":0,"hightotal":0,"highfree":0,"lowtotal":0,"lowfree":0,"swaptotal":0,"swapfree":0,"mapped":0,"vmalloctotal":0,"vmallocused":0,"vmallocchunk":0,"hugepagestotal":0,"hugepagesfree":0,"hugepagesize":0}

对进程的管理:

	p, _ := process.NewProcess(pid) // Specify process id of parent

	ps, _ := p.Children()
	for _, v := range ps{
		err = v.Kill()  // Kill each child
		// handle error
	}

	p.Kill() // Kill the parent process

还有其它各种系统信息。。。。。。

gopsutil https://github.com/shirou/gopsutil 8

本文网址: https://www.golangnote.com/topic/244.html (转载注明出处)
关于GolangNote:记录在工作中使用golang 遇到、面临的相关问题及解决方法。如果你在这里获得一些知识或信息,解决你的编程问题,请考虑捐赠给不幸的人或者你喜欢的慈善机构,除捐赠外,种植树木、志愿服务或减少排碳的行为也很有益处。