I am working on linux.

Is there any way to get the user defined program name, given the PID of that running program?

I want to output the program name, not the process name.

For example: I have a java application, named as stackoverflow.java. Now the process name will be decided by the system which can be different but the program name is stackoverflow.java. So the output should be the program name, given only the PID of that running program.

There are some commands which are fulfilling partial needs like:

cat /proc/"PID"/cmdline -> This will give the command line arguments that creates that process with given "PID". But if we have various programs in different programming languages then the format of the command which runs that program will not be same. So in that case, how to extract the exact program name from this command?

readlink -f /proc/"PID"/exe -> This will give the executable file name related to the process with given "PID". But some processes do not have executable files. In that case, it will not return anything.

Actually I am working on very large scale. I want to find the application name (or source file name or program name) for all the processes that are running on the server. So I don't want to hard code anything specifically for any programming language like java, cpp, python etc. Please suggest me something generic in this regard.

closed as unclear what you're asking by Gilles, countermode, Rui F Ribeiro, Jenny D, user34720 Jun 28 '17 at 10:54

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    You want to get the name of any source files which may have been compiled to produce the running executable? – user4556274 Jun 27 '17 at 13:52
  • @user4556274 Yes exaclty, I want to find the name of the source file by using the PID of running executables. – Aditya Jun 27 '17 at 13:55
  • 1
    You will need: ps -q 3300 -o comm= where 3300 is your process ID – Valentin Bajrami Jun 27 '17 at 14:11
  • 1
    How do you define “program name”? There is no such technical concept. It's a phrase that you're using, but it has no standard meaning, so you need to define it. – Gilles Jun 27 '17 at 22:49
  • @Gilles Sorry for inconvenience caused. By program name, I mean to say the source file's name which creates the process with given PID. – Aditya Jun 28 '17 at 5:19

Try this with a valid <pid>,

/bin/cat /proc/<pid>/comm

That would give the name of the executable.

/bin/ls -lt /proc/<pid>/exe

The above will give the path of the executable.

  • 1
    +1, but why /bin/...? – pfnuesel Jun 27 '17 at 15:02
  • @pfnuesel, just not to get into the context of aliased (alias) commands. – sai sasanka Jun 27 '17 at 15:07
  • ... and the result of this will be "java" - not what he is looking for methinks. – Murray Jensen Jun 28 '17 at 2:34
  • @MurrayJensen , right, and the command line arguments for java can be found from either doing a cat /proc/<pid>/cmdline or cat /proc/<pid>/environ – sai sasanka Jun 28 '17 at 4:34
  • @saisasanka Thanks a lot for your efforts. Actually I am working on very large scale. I want to find the application name (or source file name or program name) for all the processes that are running on the server. So I don't want to hard code anything specifically for any programming language. Please suggest me something in this regard. Thank you. – Aditya Jun 28 '17 at 5:33

You're trying to define a concept that just doesn't exist. You aren't going to find a general concept of “program name” or “application name” that goes beyond what you can find as the name of the executable.

The source file names are not, in general, stored in the executable. For a script the “primary” source file name is the executable, but a script also uses code from a lot of other places (the interpreter at least). For a compiled program the information is not available at runtime unless something specific to the compiler or build chain puts it there, and most don't.

Looking at /proc/$pid/exe gives the path to the executable. All processes do have an executable¹. The executable may have been renamed or deleted since the process was started. That's the only universal concept of “program name”.

The first element of /proc/$pid/cmdline is argument 0 passed when invoking the process. This name is chosen by the caller of the process. Unless the caller went out of its way, this is normally the base name of the executable, with or without a directory part depending on whether the program was invoked through a PATH lookup. A process can overwrite this part but few programs do (mostly, a few daemons do this to reflect their status in a way that can easily be seen with ps).

Linux also stores a process name that's reported by ps -o comm. It's accessible through /proc/$pid/status. That's the initial base name of the executable, truncated to 16 bytes.

¹ There are entries in /proc whose exe link is not readable. Those are kernel threads or zombies, not processes. For all processes, the link is readable (with proper permission), though it might not point to an existing file if the executable has been deleted.

  • Thank you. Now I got the concept. Actually when I am executing the command: /proc/$pid/cmdline on the server, then it is listing out some services running on that server. I want to get the service names. Is there any way to get that? Currently I am trying by looking at the .xml file that I am getting from the command line arguments in /proc/$pid/cmdline command. But challenge is to write the generic code that lists out the service names. – Aditya Jun 28 '17 at 7:34
  • @Aditya Define “service name”... – Gilles Jun 28 '17 at 9:14
  • Service name means name of tomcat services. – Aditya Jun 28 '17 at 11:14
  • @Aditya Ok, “tomcat service” has a precise definition, I think. I have no idea how they work, though. Please ask a new question specifically about tomcat services. – Gilles Jun 28 '17 at 12:31
  • Ok I will put more clear question now. Thanks for your efforts. – Aditya Jun 29 '17 at 8:37

There will be no general solution for this, because it is not always possible.  If I have a C program, blue.c, and I compile it with

cc blue.c -o green

and then run the green executable, there will be no way to determine that the source name was blue.c, short of forensic measures like searching my directory.  (This statement is true for at least some C compilers.)

Not the answer you're looking for? Browse other questions tagged or ask your own question.