Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Process name from its pid in linux

How to get a process name from his pid ? For example I execute cat file1.txt, but I want to figure out that cat command and its arguments since its pid in the system. Is there a struct to determine it or something similar? Any idea?

like image 884
TheForbidden Avatar asked Mar 21 '13 10:03

TheForbidden


2 Answers

There is not any general way to do this unix.
Each OS has different ways to handle it and some are very hard. You mention Linux though. With Linux, the info is in the /proc filesystem.
To get the command line for process id 9999, read the file /proc/9999/cmdline.

like image 161
Anubhab Avatar answered Oct 07 '22 11:10

Anubhab


On linux, you can look in /proc/. Try typing man proc for more information. The contents of /proc/$PID/cmdline will give you the command line that process $PID was run with. There is also /proc/self for examining yourself :)

An alternative (e.g. on Mac OS X) is to use libproc. See libproc.h.

like image 38
robbie_c Avatar answered Oct 07 '22 10:10

robbie_c