当前位置: 首页>>代码示例>>Golang>>正文


Golang Process.Pid方法代码示例

本文整理汇总了Golang中os.Process.Pid方法的典型用法代码示例。如果您正苦于以下问题:Golang Process.Pid方法的具体用法?Golang Process.Pid怎么用?Golang Process.Pid使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在os.Process的用法示例。


在下文中一共展示了Process.Pid方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: cleanup

func cleanup(key string, log plugin.Logger) error {
	pids, err := listProc()
	if err != nil {
		return err
	}
	pidMarker := fmt.Sprintf("EVR_AGENT_PID=%v", os.Getpid())
	taskMarker := fmt.Sprintf("EVR_TASK_ID=%v", key)
	for _, pid := range pids {
		env, err := getEnv(pid)
		if err != nil {
			continue
		}
		if envHasMarkers(env, pidMarker, taskMarker) {
			p := os.Process{}
			p.Pid = pid
			if err := p.Kill(); err != nil {
				log.LogSystem(slogger.INFO, "Cleanup killing %v failed: %v", pid, err)
			} else {
				log.LogTask(slogger.INFO, "Cleanup killed process %v", pid)
			}
		}
	}
	return nil
}
开发者ID:himanshugpt,项目名称:evergreen,代码行数:24,代码来源:subtree_solaris.go

示例2: cleanup

func cleanup(key string, log plugin.Logger) error {
	/*
		Usage of ps on OSX for extracting environment variables:
		-E: print the environment of the process (VAR1=FOO VAR2=BAR ...)
		-e: list *all* processes, not just ones that we own
		-o: print output according to the given format. We supply 'pid,command' so that
		only those two columns are printed, and then we extract their values using the regexes.

		Each line of output has a format with the pid, command, and environment, e.g.:
		1084 foo.sh PATH=/usr/bin/sbin TMPDIR=/tmp LOGNAME=xxx
	*/

	out, err := exec.Command("ps", "-E", "-e", "-o", "pid,command").CombinedOutput()
	if err != nil {
		log.LogSystem(slogger.ERROR, "cleanup failed to get output of 'ps': %v", err)
		return err
	}
	myPid := fmt.Sprintf("%v", os.Getpid())

	pidsToKill := []int{}
	lines := strings.Split(string(out), "\n")

	// Look through the output of the "ps" command and find the processes we need to kill.
	for _, line := range lines {
		// Use the regexes to extract the fields look for our 'tracer' variables
		matchTask := taskEnvRegex.FindStringSubmatch(line)
		matchAgent := agentPidRegex.FindStringSubmatch(line)
		if matchTask == nil || matchAgent == nil {
			continue
		}
		pidStr := matchTask[1]
		taskId := matchTask[2]
		agentPid := matchAgent[2]

		// If the process is from a different task, agent process,
		// or is the agent itself, leave it alone.
		if pidStr == myPid || taskId != key || agentPid != myPid {
			continue
		}

		// Otherwise add it to the list of processes to clean up
		pidAsInt, err := strconv.Atoi(pidStr)
		if err != nil {
			continue
		}

		pidsToKill = append(pidsToKill, pidAsInt)
	}

	// Iterate through the list of processes to kill that we just built, and actually kill them.
	for _, pid := range pidsToKill {
		p := os.Process{}
		p.Pid = pid
		err := p.Kill()
		if err != nil {
			log.LogSystem(slogger.ERROR, "Cleanup got error killing pid %v: %v", pid, err)
		} else {
			log.LogSystem(slogger.INFO, "Cleanup killed pid %v", pid)
		}
	}
	return nil

}
开发者ID:himanshugpt,项目名称:evergreen,代码行数:63,代码来源:subtree_darwin.go


注:本文中的os.Process.Pid方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。