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 把cookie 字符串解析为cookie 结构

在做爬虫时有时候会遇到需要带已登录的 cookie 请求,这个时候最简单的方法是在浏览器登录后,在开发者面板找到cookie 字符串,然后拷贝粘贴。这就面临一个问题需要把cookie 字符串解析成Go 语言 cookie 结构体。...

谷歌翻译的 golang 库推荐

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

Golang实现简单的Socks5代理

Socks5 代理较 `http/https` 代理有较好的性能,下面是借鉴某个著名开源软件的 local 实现的简单代理。...

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

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