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
c - How to get current process name in linux? - Stack Overflow
Recommended Posts