检测进程是否存在


//检查指定进程是否存在
int detect_process(const char * process_name)    
{    
    int n = -1;   
      
    FILE *strm;    
    char buf[128];    

    sprintf(buf,"ps -e | grep -c %s", process_name);    
      
    if((strm=popen(buf, "r")) != NULL)    
    {    
        if(fgets(buf, sizeof(buf), strm) != NULL)    
        {
        	n = atoi(buf); 
			if(n > 2)
			{
				return n;
			}
        }
    }
	else  
    {  
        return -1;  
    }         
      
    pclose(strm);     
       
    return 0;    
}
其中 

if(n > 2) 2这个条件可能需要根据系统调整

关于ps命令的参数

-e 显示所有程序

grep用来筛选想要的信息

grep -c用来统计匹配查询内容的行数


你可能感兴趣的:(编程语言)