Get own process name at runtime in C / C ++ (for Linux)

Summary of how to get your own process name at runtime in C, C ++

get_process_name.cpp


#include <iostream>
#include <cstdlib>
#include <errno.h>

extern char *__progname;

int main(int argc, char const* argv[])
{
        // argv[0]
        std::cout << "argv[0] : " << argv[0] << std::endl;

        // linux environment
        std::cout << "getenv(\"_\") : " << getenv("_") << std::endl;

        // libc
        std::cout << "__progname : " << __progname << std::endl;

        // errno.h
        std::cout << "program_invocation_name : " << program_invocation_name << std::endl;
        std::cout << "program_invocation_short_name : " << program_invocation_short_name << std::endl;

        return 0;
}

Execution example


/tmp$ g++ get_process_name.cpp -o hoge && ./hoge
argv[0] : ./hoge
getenv("_") : ./hoge
__progname : hoge
program_invocation_name : ./hoge
program_invocation_short_name : hoge

/tmp$ g++ get_process_name.cpp -o hoge && $PWD/hoge
argv[0] : /tmp/hoge
getenv("_") : /tmp/hoge
__progname : hoge
program_invocation_name : /tmp/hoge
program_invocation_short_name : hoge

reference

c - How to get current process name in linux? - Stack Overflow

Recommended Posts

Get own process name at runtime in C / C ++ (for Linux)
[Linux] [C] Get CPU time per process / thread
Library for measuring execution time in Linux C applications
Get the host name in Python
Guidelines for reincarnating in the world of linux programming development (C / C ++ language)
Create your own Linux commands in Python
Implement part of the process in C ++
Windows → linux Tips for bringing in data
Process multiple lists with for in Python
Get query parameters for GET requests in Django
Solution for run-time error in OpenVINO IEPlugin.load ()
Get a token for conoha in python
Get your own IP address in Python
Communicate with I2C devices in Linux C