Linux 停止当前正在运行程序

1、终端执行shell命令

得到进程PID
ps -e | grep ‘test’ | awk ‘{print $1}’
12478
其中test为进程名
杀进程
kill -9 12478

2、代码

    #include 
    #include 
    #include 

    void stop()
    {
        FILE *fp = popen("ps -e | grep \'test\' | awk \'{print $1}\'", "r");
        char buffer[10] = {0};

        while(NULL != fgets(buffer, 10, fp))
        {
                printf("PID: %s\n", buffer);
                char command[1024] = "kill -9 ";
                strcat(command, buffer);
                popen(command, "r");
        }

        pclose(fp);

    }

你可能感兴趣的:(linux)