golang操作进程包,获取进程和杀死进程等操作,推荐一个常用包
"github.com/shirou/gopsutil/process"
比如我要打印所有进程名字和ID如下所示:
type Pdata struct {
Pid int32
Pname string
}
func main() {
pName := ProcessName()
for _, v := range pName {
fmt.Println("进程名:", v)
}
}
/*
获取所有进程名,以数组返回
*/
func ProcessName() (pname []Pdata) {
pids, _ := process.Pids()
for _, pid := range pids {
rtn := new(Pdata)
rtn.Pid = pid
pn, _ := process.NewProcess(pid)
pName, _ := pn.Name()
rtn.Pname = pName
pname = append(pname, *rtn)
}
return pname
}
以下是此包常用方法
Index ¶
Constants
Variables
func PidExists(pid int32) (bool, error)
func PidExistsWithContext(ctx context.Context, pid int32) (bool, error)
func Pids() ([]int32, error)
func PidsWithContext(ctx context.Context) ([]int32, error)
type IOCountersStat
type MemoryInfoExStat
type MemoryInfoStat
type MemoryMapsStat
type NumCtxSwitchesStat
type OpenFilesStat
type PageFaultsStat
type Process
func (p *Process) Background() (bool, error)
func (p *Process) BackgroundWithContext(ctx context.Context) (bool, error)
func (p *Process) CPUAffinity() ([]int32, error)
func (p *Process) CPUAffinityWithContext(ctx context.Context) ([]int32, error)
func (p *Process) CPUPercent() (float64, error)
func (p *Process) CPUPercentWithContext(ctx context.Context) (float64, error)
func (p *Process) Children() ([]*Process, error)
func (p *Process) ChildrenWithContext(ctx context.Context) ([]*Process, error)
func (p *Process) Cmdline() (string, error)
func (p *Process) CmdlineSlice() ([]string, error)
func (p *Process) CmdlineSliceWithContext(ctx context.Context) ([]string, error)
func (p *Process) CmdlineWithContext(ctx context.Context) (string, error)
func (p *Process) Connections() ([]net.ConnectionStat, error)
func (p *Process) ConnectionsMax(max int) ([]net.ConnectionStat, error)
func (p *Process) ConnectionsMaxWithContext(ctx context.Context, max int) ([]net.ConnectionStat, error)
func (p *Process) ConnectionsWithContext(ctx context.Context) ([]net.ConnectionStat, error)
func (p *Process) CreateTime() (int64, error)
func (p *Process) CreateTimeWithContext(ctx context.Context) (int64, error)
func (p *Process) Cwd() (string, error)
func (p *Process) CwdWithContext(ctx context.Context) (string, error)
func (p *Process) Environ() ([]string, error)
func (p *Process) EnvironWithContext(ctx context.Context) ([]string, error)
func (p *Process) Exe() (string, error)
func (p *Process) ExeWithContext(ctx context.Context) (string, error)
func (p *Process) Foreground() (bool, error)
func (p *Process) ForegroundWithContext(ctx context.Context) (bool, error)
func (p *Process) Gids() ([]int32, error)
func (p *Process) GidsWithContext(ctx context.Context) ([]int32, error)
func (p *Process) Groups() ([]int32, error)
func (p *Process) GroupsWithContext(ctx context.Context) ([]int32, error)
func (p *Process) IOCounters() (*IOCountersStat, error)
func (p *Process) IOCountersWithContext(ctx context.Context) (*IOCountersStat, error)
func (p *Process) IOnice() (int32, error)
func (p *Process) IOniceWithContext(ctx context.Context) (int32, error)
func (p *Process) IsRunning() (bool, error)
func (p *Process) IsRunningWithContext(ctx context.Context) (bool, error)
func (p *Process) Kill() error
func (p *Process) KillWithContext(ctx context.Context) error
func (p *Process) MemoryInfo() (*MemoryInfoStat, error)
func (p *Process) MemoryInfoEx() (*MemoryInfoExStat, error)
func (p *Process) MemoryInfoExWithContext(ctx context.Context) (*MemoryInfoExStat, error)
func (p *Process) MemoryInfoWithContext(ctx context.Context) (*MemoryInfoStat, error)
func (p *Process) MemoryMaps(grouped bool) (*[]MemoryMapsStat, error)
func (p *Process) MemoryMapsWithContext(ctx context.Context, grouped bool) (*[]MemoryMapsStat, error)
func (p *Process) MemoryPercent() (float32, error)
func (p *Process) MemoryPercentWithContext(ctx context.Context) (float32, error)<