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 时区时差处理方式

个人习惯用 0 时区时间戳记录时间,可以方便转到不同时区,下面介绍 Golang 时区时差处理...

Golang sync.WaitGroup 的 Wait 超时处理

sync.WaitGroup 使用 `Add(1)`、`Done()`、`Wait()`组合来实现多协程等待,如果某一协程未能合理处理错误,导致无法退出,此时需要引入超时机制。下面是一种超时处理方法。...

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

Submit Comment Login
Based on Golang + fastHTTP + sdb | go1.22.3 Processed in 0ms