GolangNote

Golang笔记

Golang 进程管理工具: gopsutil

Permalink

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

下面是对内存的显示:

Go: 显示内存
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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)
}

在我的电脑上输出:

JSON: 内存状态
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
{
	"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
}

对进程的管理:

Go: 管理进程
1
2
3
4
5
6
7
8
9
10
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

本文网址: https://golangnote.com/topic/244.html 转摘请注明来源

Related articles

谷歌翻译的 golang 库推荐

Google 的翻译越来越好了,官方的Golang SDK 已经很完美,这里介绍的是几个非官方发布的有特色的库。...

Golang http IPv4/IPv6 服务

Golang 的网络服务,如果不指定IPv4 或 IPv6,如果VPS 同时支持 IPv4 和 IPv6,`net.Listen()` 只会监听 IPv6 地址。但这不影响客户端使用 IPv4 地址来访问。...

Write a Comment to "Golang 进程管理工具: gopsutil"

Submit Comment Login
Based on Golang + fastHTTP + sdb | go1.17 Processed in 1ms