¿Cómo obtener el nombre del proceso actual en 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?

preguntado el 01 de febrero de 12 a las 14:02

8 Respuestas

If you're on using a glibc, then:

#define _GNU_SOURCE
#include <errno.h>

extern char *program_invocation_name;
extern char *program_invocation_short_name;

See program_invocation_name(3)

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

Respondido 10 Abr '14, 07:04

It's either pointed to by the argv[0] or indeed you can read /proc/self/status. O puedes usar getenv("_"), not sure who sets that and how reliable it is.

Respondido 01 Feb 12, 18:02

Tenga en cuenta que 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. - Roger Lipscombe

Puede usar el __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);

Respondido el 19 de enero de 18 a las 13:01

I often make use of following call,

char* currentprocname = getprogname();

Respondido 18 Feb 13, 14:02

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

Mira el valor de argv[0] which was passed to main. This should be the name under which your process was invoked.

Respondido 01 Feb 12, 18:02

Unfortunatelly in this case I dont have access to argv[0] - Mariusz

@Mariusz Then you're going to have to go through proc - although I recommend /proc/self/cmdline. - Borealido

@Mariusz, You can also try getenv("_") though I'm not sure how reliable it is. - Michael Krelin - hacker

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;
const char * appname = "?";

Respondido 18 Feb 17, 13:02

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 aquí.

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.

Respondido 13 Jul 14, 08:07

or you could look at /proc/self/cmdline - Erin

for posterity, a version that's more C++-ish and works also on MSVC:


#include <fmt/format.h>

std::string get_current_process_name()
    #if defined(__APPLE__) || defined(__FreeBSD__)
        return getprogname();
    #elif defined(_GNU_SOURCE)
        return program_invocation_name;
    #elif defined(_WIN32)
        return __argv[0];
        return "?";

int main()
    fmt::print("whatsmyname: {}\n", get_current_process_name());
    return 0;

// msvc output:
// whatsmyname: C:\Users\<user>\source\repos\Project6\Debug\Project6.exe

Respondido 28 Feb 21, 16:02

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.