Linux修改进程名

  • 方法:把argv[0](环境变量)之后的参数全部删除,然后覆盖
#include<stdio.h>
#include<unistd.h>
#include<string.h>
#include<stdlib.h>

#define  PROCESS_NAME "Ctracer2333helloworld dongdong"

int main(int argc,char* argv[])
{
    printf("before:---------------------------------------------\n");
    //system函数:实现在程序中运行shell命令
    system("ps -ef|grep First");
    //clear argv[1,2,3,,] 
    for(int i=1;i<argc;i++)
       memset(argv[i],' ',strlen(argv[i]));
    //覆盖argv[0],原argv[0]指向的是程序路径及名称(环境变量)
    strcpy(argv[0],PROCESS_NAME);
    printf("after:----------------------------------------------\n");
    system("ps -ef|grep Ctracer");
    return 0;
}

运行

Last modification:June 16th, 2021 at 08:53 pm