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?
7 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
.

- 1,736
- 2
- 18
- 28
-
13And to get the process name for process id 9999, read the file `/proc/9999/comm`. – Skippy le Grand Gourou Jul 05 '13 at 15:02
-
1The question was how to get the process name, not the commandline. On my SUSE I enter 'head /proc/9999/cmdline' for a kwrite process and it returns: 'kdeinit4: kwrite [kdeinit]'. If I use /comm instead of /cmdline I get the correct result: 'kwrite'. – Elmue Apr 21 '15 at 11:01
-
1`/proc/$pid/comm` trims the output to 15 chars, it seems. This is very... very bad... – Artfaith Jun 01 '21 at 03:58
-
2@SkippyleGrandGourou `/proc/
/comm` will be truncated if it reaches 16 chars or TASK_COMM_LEN, a better way is to read `realpath` of `/proc/ – KuhakuPixel Nov 15 '22 at 11:04/exe` as shown [here](https://unix.stackexchange.com/a/629871/505340)
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.

- 2,428
- 1
- 19
- 28
-
Can you please tell me the difference between your answer and mine? :) – Anubhab Mar 21 '13 at 11:02
-
17
POSIX C does NOT support give a standard API for getting the process name by PID.
In linux, you can get the name by LINUX Proc API: /proc/$PID/cmdline. And the code looks like these:
const char* get_process_name_by_pid(const int pid)
{
char* name = (char*)calloc(1024,sizeof(char));
if(name){
sprintf(name, "/proc/%d/cmdline",pid);
FILE* f = fopen(name,"r");
if(f){
size_t size;
size = fread(name, sizeof(char), 1024, f);
if(size>0){
if('\n'==name[size-1])
name[size-1]='\0';
}
fclose(f);
}
}
return name;
}

- 907
- 8
- 10
-
1And if you came here looking for a solution in C# working with mono, then have a look at this: http://stackoverflow.com/questions/29827331/workaround-for-bug-in-mono-wrong-process-processname-solved – Elmue Apr 23 '15 at 15:32
To get the process name of a process id say 9000 use this command:
ps -p 9000 -o comm=

- 2,994
- 1
- 24
- 19
While this question has been answered, I'd like to add my 2 cents.
In my case, when process 1111
creates process 22222
via pipe
(at least this is what I heard), /proc/2222/cmdline
does not give correct process name, but instead gives something like 1111_1
. I have to use /proc/2222/comm
to get the correct process name.
ps --pid <pid> -o comm h
:
This command gives executable file name. For example if you run a script name.sh, then the above command gives output as bash
ps --ppid <pid> -o comm h
:
This command gives the output as name

- 11
- 8