C实现进程名查找pid并结束进程

FILE *fp = popen("ps | grep \'run_name\' | grep -v grep | awk \'{print $1}\'", "r"); 
char buffer[10] = {0};                                                                
while(NULL != fgets(buffer, 10, fp)){                                                 
    printf("PID: %s\n", buffer);                                                      
    if (buffer > 0)                                                                   
    {                                                                                 
        int pid;                                                                      
        sscanf(buffer, "%d", &pid);                                                   
        kill(pid, SIGKILL);                                                           
    }                                                                                 
}                                                                                     
pclose(fp);                                                                           

 

你可能感兴趣的:(C实现进程名查找pid并结束进程)