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?
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
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With