當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。