Linux get application name

How to get the actual program name using the PID of that running program? [closed]

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.

You want to get the name of any source files which may have been compiled to produce the running executable?

@user4556274 Yes exaclty, I want to find the name of the source file by using the PID of running executables.

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 Sorry for inconvenience caused. By program name, I mean to say the source file’s name which creates the process with given PID.

3 Answers 3

That would give the name of the executable.

The above will give the path of the executable.

Читайте также:  Создать загрузочную флешку uefi linux

@MurrayJensen , right, and the command line arguments for java can be found from either doing a cat /proc//cmdline or cat /proc//environ

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

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.

Источник

How to get the actual program name using the PID of that running program?

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.

Читайте также:  Linux disk cloning tool

Your best bet is looking at /proc/PID/cmdline and write if conditions depending on the first or second name. Like if it is python take second. If it is an executable, first one etc.

Stack Overflow is a site for programming and development questions. This question appears to be off-topic because it is not about programming or development. See What topics can I ask about here in the Help Center. Perhaps Super User or Unix & Linux Stack Exchange would be a better place to ask.

Источник

How to find out the name and path of a running gui application on linux?

I am interested to learn more about my system. Started for example a GUI application «Printing». A shiny nice name in the title. There is no file «printing» in /usr/bin. Is there some trick to show the real name of the application in the filesystem instead of «printing», for example «printgui»?

2 Answers 2

Usually GUI applications are started through «launchers», i.e. .desktop files. You are seeing «Printing» in your menus instead of the real name because that is the «generic name» of the application (details in the link I posted). Depending on what DE you are using you might be able to right click on the menu entry and select Edit or Properties which would allow you to see the executable path and sometimes the real name for that app. That’s not possible in a DE like Gnome 3 so here is a solution that should work regardless of your DE.

.desktop files are located in /usr/share/applications so simply grep -ing for GenericName=Printing should list at least one launcher containing that string.

Читайте также:  Add new group to linux

E.g. I don’t have «Printing» on my system but I have «Text Editor». I want to know the real name of the application/executable so I run:

grep "GenericName=Text Editor" /usr/share/applications/* 
/usr/share/applications/gedit.desktop:GenericName=Text Editor 

So the «launcher» for «Text Editor» is /usr/share/applications/gedit.desktop . Now, in order to know the real name and executable for «Text Editor» I have to grep the value of two keys: Name and Exec :

grep -E '(^Name=|^Exec=)' /usr/share/applications/gedit.desktop 

For the full path of the executable, it’s only a matter or running:

Источник

How to get current process name in linux?

How can I get the process name in C? The same name, which is in /proc/$pid/status . I do not want to parse that file. Is there any programmatic way of doing this?

8 Answers 8

If you’re on using a glibc, then:

#define _GNU_SOURCE #include extern char *program_invocation_name; extern char *program_invocation_short_name; 

Under most Unices, __progname is also defined by the libc. The sole portable way is to use argv[0]

It’s either pointed to by the argv[0] or indeed you can read /proc/self/status . Or you can use getenv(«_») , not sure who sets that and how reliable it is.

Note that getenv(«_») appears to return the process originally started by the shell — if I call it in a process started by make , I see «/usr/bin/make», rather than my process name. This means that it’s probably set by the shell.

You can use __progname . However it is not better than argv[0] as it may have portability issues. But as you do not have access to argv[0] it can work as follows:-

extern char *__progname; printf("\n%s", __progname); 

I often make use of following call,

char* currentprocname = getprogname(); 

That’s BSD-specific. You can get it on Linux with libbsd , but it’s not part of libc as it is on FreeBSD or OS X.

Look at the value of argv[0] which was passed to main . This should be the name under which your process was invoked.

This is a version that works on macOS, FreeBSD and Linux.

#if defined(__APPLE__) || defined(__FreeBSD__) const char * appname = getprogname(); #elif defined(_GNU_SOURCE) const char * appname = program_invocation_name; #else const char * appname = "?"; #endif 

If you cannot access argv[] in main(), because you are implementing a library, you can have a look at my answer on a similar question here.

It basically boils down into giving you access to argc, argv[] and envp[] outside of main(). Then you could, as others have already correctly suggested, use argv[0] to retrieve the process name.

Источник

Оцените статью
Adblock
detector