Linux C语言检查进程是否存在

使用C语言根据进程名检查进程是否存在,然后重启进程


/*
*  COPYRIGHT NOTICE
*  Copyright (C) 2016 HuaHuan Electronics Corporation, Inc. All rights reserved
*
*  Author       	:Kevin_fzs
*  File Name        	:/home/kevin/works/projects/MIPS53003/drivers/webRestart.c
*  Create Date        	:2016/08/04 01:11
*  Last Modified      	:2016/08/04 01:11
*  Description    	:
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int getRestartStatus()
{
	char *name="/home/webserver/Rflag.txt";	
	FILE *fd;
	int ret=0;	

	fd = fopen(name, "r");
	if(NULL == fd)
		return 1;
	else
		return 0;
}

int main()
{
	FILE *ptr = NULL;
	char cmd[128] = "ps -ef | grep appweb | grep -v grep | wc -l";
	int status = 0;
	char buf[150];
	int count;
	
	while(1)
	{
		status = getRestartStatus();<span style="white-space:pre">				</span>//根据标志文件来决定是否要检查进程
		if(status)
		{
			if((ptr = popen(cmd, "r"))==NULL)
			{
				printf("popen err\n");	
				continue;
			}

			memset(buf, 0, sizeof(buf));

			if((fgets(buf, sizeof(buf),ptr))!= NULL)<span style="white-space:pre">		</span>//获取进程和子进程的总数
			{
				count = atoi(buf);
				if(count <= 0)<span style="white-space:pre">			</span>//当进程数小于等于0时,说明进程不存在
				{
					system("/home/appweb_start.sh");
					printf("restart appweb \n");
				}
					
			}
			
		}
		usleep(200000);
	}
}









  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值