在Linux上更改C中的实际进程名称

das*_*s_j 6 c linux process

我目前正在尝试更改进程的进程名称,以便我可以更轻松地使用htop,top,....我希望LD_PRELOAD将此代码转换为另一个进程,以便通过environemt变量重命名.

我在互联网上发现了很多东西,但没有任何作用:

prctl(PR_SET_NAME, "Test");
Run Code Online (Sandbox Code Playgroud)

这不起作用,因为htop不尊重名称.

Nginx setproctitle(链接)也不起作用,因为它剥离了参数(进程需要的参数).

我尝试了所有我发现的东西,现在我没有想法.

这在Linux中甚至可能吗?如何?

Sla*_*ica 10

只需通过shell脚本或程序运行您的程序exec,并将所需的名称传递给argv[0]:

#/bin/bash
exec -a fancy_name a.out ...
Run Code Online (Sandbox Code Playgroud)

或者C/C++:

execl( "./a.out", "fancy_name", ... );
Run Code Online (Sandbox Code Playgroud)

  • 问题是关于“c++”而不是“bash”或运行。 (2认同)
  • 在顶部它仍然显示实际的进程名称而不是 fancy_name (2认同)

ber*_*ing 7

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#define NEW_NAME "hello_world"
int main(int argc, char **argv) {
  if(strcmp(argv[0], NEW_NAME)) {
    argv[0] = NEW_NAME;
    execv("/proc/self/exe", argv);
    fputs("exec failed", stderr); 
    return 1;
  }
  while(1) // so it goes to the top
    ;
}
Run Code Online (Sandbox Code Playgroud)