33

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?

TheForbidden
  • 1,533
  • 4
  • 22
  • 30
  • 1
    possible duplicate of [Get process name by PID](http://stackoverflow.com/questions/4189717/get-process-name-by-pid) – Hasturkun Mar 21 '13 at 11:01

7 Answers7

29

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.

Anubhab
  • 1,736
  • 2
  • 18
  • 28
  • 13
    And 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
  • 1
    The 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//exe` as shown [here](https://unix.stackexchange.com/a/629871/505340) – KuhakuPixel Nov 15 '22 at 11:04
16

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.

robbie_c
  • 2,428
  • 1
  • 19
  • 28
15

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;
}
QJGui
  • 907
  • 8
  • 10
  • 1
    And 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
13

To get the process name of a process id say 9000 use this command:

ps -p 9000 -o comm=
Thunder
  • 2,994
  • 1
  • 24
  • 19
3

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.

hola
  • 930
  • 1
  • 17
  • 35
toddwz
  • 521
  • 4
  • 9
2

Use the below command in Linux

ls -l /proc/[pid]/exe

It will give the name of the process/application name

Paul Roub
  • 36,322
  • 27
  • 84
  • 93
Vikram B
  • 117
  • 1
  • 6
1

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

inihsrah
  • 11
  • 8