Reputation: 1843
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?
Upvotes: 179
Views: 484984
Reputation: 2802
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
Upvotes: 43
Reputation: 842
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)
Upvotes: 43
Reputation: 1698
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
Upvotes: 4
Reputation: 1609
Just type this and you will get what you want. Replace 'type_PID_here' with the PID.
cat /proc/type_PID_here/status
Upvotes: 65
Reputation: 206
ps -p PID -o comm=
Enter the code above where PID is PID of the process.
Upvotes: 16
Reputation: 34074
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:
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.
Upvotes: 190
Reputation: 166588
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
Upvotes: 6