Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get process details from its PID

Tags:

go

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?

like image 525
geek Avatar asked Feb 21 '12 12:02

geek


People also ask

How get PID process details?

The ps -p <PID> command is pretty straightforward to get the process information of a PID. Alternatively, we can also access the special /proc/PID directory to retrieve process information.

How can we get process details using PID in Unix?

The easiest way to find out if process is running is run ps aux command and grep process name. If you got output along with process name/pid, your process is running.


Video Answer


2 Answers

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.

like image 96
user56reinstatemonica8 Avatar answered Oct 15 '22 02:10

user56reinstatemonica8


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

cat /proc/type_PID_here/status 
like image 45
Saroj Rai Avatar answered Oct 15 '22 02:10

Saroj Rai