如何从进程ID获取进程详细信息

huangapple go评论91阅读模式
英文:

How to get process details from its PID

问题

我已经维护了一个当前正在运行在我的系统上的进程的PID列表(Linux)。现在,如果我能从这个PID获取进程的详细信息就太好了。我已经在Go中找到了syscall.Getrusage(),但是我没有得到期望的结果。

我应该怎么办?

英文:

I have maintained a list of PIDs of processes currently running on my system (Linux). From this, now it would be great if I could get the process details from this PID. I have come over syscall.Getrusage() in Go, but I am not getting the desired results.

What should I do?

答案1

得分: 161

这可能不完全是提问者想要的(关于每个进程ID需要哪些详细信息没有太多明确的信息),但是您可以使用BASH命令ps -p $PIDpsprocess status的缩写)获取任务的一些详细信息。

使用默认选项ps -p $PID,返回的信息包括:

  • PID:显示进程ID
  • TTY:控制终端的名称(如果有的话)
  • TIME:进程自执行以来使用的CPU时间(例如00:00:02)
  • CMD:调用进程的命令(例如java

使用-o选项标志可以显示有关此进程ID的更多信息。有关列表,请参阅此文档页面

以下是一个示例,它告诉您特定进程PID的完整命令及其参数、用户、组和内存使用情况(请注意,多个-o标志每个都需要一对,以及命令输出带有大量空格填充):

ps -p $PID -o pid,vsz=MEMORY -o user,group=GROUP -o comm,args=ARGS

提示:为了在控制台上以人类可读的输出形式显示,将args作为最后一个选项 - 它通常是最长的,否则可能会被截断。

英文:

This might not be exactly what the asker wanted (there's not much clear info on what type of details are required for each process id), but you can get some details of a task by its pid using the BASH command ps -p $PID (ps being short for process status)

With default options as ps -p $PID this returns:

  • PID: echos the process id
  • TTY: the name of the controlling terminal (if any)
  • TIME: how much CPU time the has process used since execution (e.g. 00:00:02)
  • CMD: the command that called the process (e.g. java)

More information about this process id can be shown using the -o options flag. For a list, see this documentation page.

Here's one example that tells you a particular process PID's full command with arguments, user, group and memory usage (note how the multiple -o flags each take a pair, and how the command outputs with lots of whitespace padding):

ps -p $PID -o pid,vsz=MEMORY -o user,group=GROUP -o comm,args=ARGS

Tip: for human-read output in the console, make args the last option - it'll usually be the longest and might get cut short otherwise.

答案2

得分: 50

只需键入此命令,您将获得您想要的内容。将'type_PID_here'替换为PID。

cat /proc/type_PID_here/status
英文:

Just type this and you will get what you want. Replace 'type_PID_here' with the PID.

cat /proc/type_PID_here/status

答案3

得分: 33

如果您想通过PID查看进程的路径,可以使用pwdx命令。pwdx命令会报告PID进程的完整路径。

$ pwdx 13896
13896: /home/user/python_program

注意:此方法仅在您具有管理进程的权限时有效(即使用root用户)。

英文:

If you want to see the path of the process by PID. You can use the pwdx command. The pwdx command reports the full path of the PID process.

$ pwdx 13896
13896: /home/user/python_program

Note: This method only works if you have the privilege to manage the process (ie. using the root user)

答案4

得分: 17

ps -p PID -o comm=

在上面的代码中,将PID替换为进程的PID。

英文:
ps -p PID -o comm=

Enter the code above where PID is PID of the process.

答案5

得分: 14

最短且最有效的选项可能是

ps -fp PID

它将返回类似以下内容:

UID            PID    PPID  C STIME TTY          TIME CMD
adam         78557    3688  0 Sep12 ?        00:00:07 /bin/python -m ipykernel_launcher -f /home/adam/.local/share/jupyter/runtime/kernel-aca88d6b.json
英文:

The shortest, and most effective option could be

ps -fp PID

it will return something like:

UID            PID    PPID  C STIME TTY          TIME CMD
adam         78557    3688  0 Sep12 ?        00:00:07 /bin/python -m ipykernel_launcher -f /home/adam/.local/share/jupyter/runtime/kernel-aca88d6b.json

答案6

得分: 7

你可以查看/proc/[pid]/stat。例如,使用Go 1,

package main

import (
	"fmt"
	"io/ioutil"
	"os"
	"strconv"
)

func Pids() ([]int, error) {
	f, err := os.Open(`/proc`)
	if err != nil {
		return nil, err
	}
	defer f.Close()
	names, err := f.Readdirnames(-1)
	if err != nil {
		return nil, err
	}
	pids := make([]int, 0, len(names))
	for _, name := range names {
		if pid, err := strconv.ParseInt(name, 10, 0); err == nil {
			pids = append(pids, int(pid))
		}
	}
	return pids, nil
}

func ProcPidStat(pid int) ([]byte, error) {
	// /proc/[pid]/stat
	// https://www.kernel.org/doc/man-pages/online/pages/man5/proc.5.html
	filename := `/proc/` + strconv.FormatInt(int64(pid), 10) + `/stat`
	return ioutil.ReadFile(filename)
}

func main() {
	pids, err := Pids()
	if err != nil {
		fmt.Println("pids:", err)
		return
	}
	if len(pids) > 0 {
		pid := pids[0]
		stat, err := ProcPidStat(pid)
		if err != nil {
			fmt.Println("pid:", pid, err)
			return
		}
		fmt.Println(`/proc/[pid]/stat:`, string(stat))
	}
}

输出:

/proc/[pid]/stat: 1 (init) S 0 1 1 0 -1 4202752 11119 405425 21 57 78 92 6643 527 20 0 1 0 3 24768512 563 184467440737095
英文:

You could look at /proc/[pid]/stat. For example, using Go 1,

package main

import (
	"fmt"
	"io/ioutil"
	"os"
	"strconv"
)

func Pids() ([]int, error) {
	f, err := os.Open(`/proc`)
	if err != nil {
		return nil, err
	}
	defer f.Close()
	names, err := f.Readdirnames(-1)
	if err != nil {
		return nil, err
	}
	pids := make([]int, 0, len(names))
	for _, name := range names {
		if pid, err := strconv.ParseInt(name, 10, 0); err == nil {
			pids = append(pids, int(pid))
		}
	}
	return pids, nil
}

func ProcPidStat(pid int) ([]byte, error) {
	// /proc/[pid]/stat
	// https://www.kernel.org/doc/man-pages/online/pages/man5/proc.5.html
	filename := `/proc/` + strconv.FormatInt(int64(pid), 10) + `/stat`
	return ioutil.ReadFile(filename)
}

func main() {
	pids, err := Pids()
	if err != nil {
		fmt.Println("pids:", err)
		return
	}
	if len(pids) > 0 {
		pid := pids[0]
		stat, err := ProcPidStat(pid)
		if err != nil {
			fmt.Println("pid:", pid, err)
			return
		}
		fmt.Println(`/proc/[pid]/stat:`, string(stat))
	}
}

Output:

/proc/[pid]/stat: 1 (init) S 0 1 1 0 -1 4202752 11119 405425 21 57 78 92 6643 527 20 0 1 0 3 24768512 563 184467440737095

答案7

得分: 2

使用终端上的ps命令获取进程的详细信息 -

ps -Flww -p THE_PROCESS_PID

要获取更多信息,请查阅此处的手册页面文档

英文:

To get the details of the process using the ps command on terminal -

ps -Flww -p THE_PROCESS_PID

For more info, checkout the documentation on the man pages here

huangapple
  • 本文由 发表于 2012年2月21日 20:58:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/9378021.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定