How to get process details from its PID

Go

Go Problem Overview


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?

Go Solutions


Solution 1 - Go

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.

Solution 2 - Go

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

cat /proc/type_PID_here/status

Solution 3 - Go

ps -p PID -o comm=

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

Solution 4 - Go

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

Solution 5 - Go

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiongeekView Question on Stackoverflow
Solution 1 - Gouser56reinstatemonica8View Answer on Stackoverflow
Solution 2 - GoSaroj RaiView Answer on Stackoverflow
Solution 3 - GoGaleeb BashaView Answer on Stackoverflow
Solution 4 - GonguyenvanhieuvnView Answer on Stackoverflow
Solution 5 - GopeterSOView Answer on Stackoverflow