用Linux守护进程检测某个程序是否运行2

用Linux守护进程检测某个程序是否运行2

 

 

本文博客链接:http://blog.csdn.net/jdh99,作者:jdh,转载请注明.


环境:

 

主机:Fedora12

目标板:SC6410

目标板LINUX内核版本:2.6.36


说明:

第一版程序(参考http://blog.csdn.net/jdh99/article/details/7300641)已经可以正常工作,不过在运行一个月后发现有两台平板出现不能启动的问题,检查后发现是nand flash坏块很多,导致系统不能启动。究其原因,因该是对flash频繁的写操作造成的。上一版本的守护程序每分钟会写操作一次,这样一天的写操作就达千次。在这一版本中,判断需要守护的进程是否存在,是通过读取/proc/pid目录来判断的。参考链接:http://kb.cnblogs.com/a/2360817/


驱动源代码:

daemon_service.c:

 

[cpp]  view plain copy
  1. //守护进程,守护AlarmInterface进程  
  2. //作者:jdh  
  3. //时间:2012-2-27  
  4. #include <stdio.h>  
  5. #include <unistd.h>  
  6. #include <sys/types.h>  
  7. #include <sys/stat.h>  
  8. #include <fcntl.h>  
  9. #include <syslog.h>  
  10. #include <dirent.h>  
  11.   
  12. //程序名字  
  13. #define NAME "AlarmInterface"  
  14. //要运行的程序  
  15. #define RUN_NAME "DuiJiang &"  
  16.   
  17. #define READ_BUF_SIZE 100  
  18.   
  19. //#define DIR_OUT_FILE "/rf/out"  
  20. //#define NAME "gnome-keyring"  
  21. //#define NAME_FIND "gnome"  
  22. //#define DIR_OUT_FILE "/root/test/out"  
  23.   
  24. int daemon(int nochdir,int noclose)  
  25. {  
  26.     pid_t pid;  
  27.   
  28.     //让init进程成为新产生进程的父进程  
  29.     pid = fork();  
  30.     //如果创建进程失败  
  31.     if (pid < 0)  
  32.     {  
  33.         perror("fork");  
  34.         return -1;  
  35.     }  
  36.     //父进程退出运行  
  37.     if (pid != 0)  
  38.     {  
  39.         exit(0);  
  40.     }  
  41.     //创建新的会话  
  42.     pid = setsid();  
  43.     if (pid < -1)  
  44.     {  
  45.         perror("set sid");  
  46.         return -1;  
  47.     }  
  48.     //更改当前工作目录,将工作目录修改成根目录  
  49.     if (!nochdir)  
  50.     {  
  51.         chdir("/");  
  52.     }  
  53.     //关闭文件描述符,并重定向标准输入,输出合错误输出  
  54.     //将标准输入输出重定向到空设备  
  55.     if (!noclose)  
  56.     {  
  57.         int fd;  
  58.         fd = open("/dev/null",O_RDWR,0);  
  59.         if (fd != -1)  
  60.         {  
  61.             dup2(fd,STDIN_FILENO);  
  62.             dup2(fd,STDOUT_FILENO);  
  63.             dup2(fd,STDERR_FILENO);  
  64.             if (fd > 2)  
  65.             {  
  66.                 close(fd);  
  67.             }  
  68.         }  
  69.     }  
  70.     //设置守护进程的文件权限创建掩码  
  71.     umask(0027);  
  72.   
  73.     return 0;  
  74. }  
  75.   
  76. //存在返回1,不存在返回0  
  77. int judge_pid_exist(char* pidName)  
  78. {  
  79.     DIR *dir;  
  80.         struct dirent *next;  
  81.         //long* pidList=NULL;  
  82.         int i=0;  
  83.   
  84.     FILE *status;  
  85.       char filename[READ_BUF_SIZE];  
  86.       char buffer[READ_BUF_SIZE];  
  87.       char name[READ_BUF_SIZE];  
  88.    
  89.       ///proc中包括当前的进程信息,读取该目录  
  90.         dir = opendir("/proc");  
  91.         if (!dir)  
  92.     {  
  93.         printf("Cannot open /proc\n");  
  94.         return 0;  
  95.     }  
  96.        
  97.         //遍历  
  98.         while ((next = readdir(dir)) != NULL)   
  99.     {  
  100.             /* Must skip ".." since that is outside /proc */  
  101.             if (strcmp(next->d_name, "..") == 0)  
  102.         {  
  103.                 continue;  
  104.         }  
  105.    
  106.             /* If it isn't a number, we don't want it */  
  107.             if (!isdigit(*next->d_name))  
  108.         {  
  109.                 continue;  
  110.         }  
  111.   
  112.                 //设置进程  
  113.             sprintf(filename, "/proc/%s/status", next->d_name);  
  114.             if (! (status = fopen(filename, "r")) )   
  115.         {  
  116.                 continue;  
  117.             }  
  118.   
  119.             if (fgets(buffer, READ_BUF_SIZE-1, status) == NULL)   
  120.         {  
  121.                 fclose(status);  
  122.                 continue;  
  123.             }  
  124.             fclose(status);  
  125.    
  126.                 //得到进程id  
  127.             /* Buffer should contain a string like "Name:   binary_name" */  
  128.             sscanf(buffer, "%*s %s", name);  
  129.             if (strcmp(name, pidName) == 0)   
  130.         {  
  131.                 //pidList=realloc( pidList, sizeof(long) * (i+2));  
  132.                 //pidList[i++]=strtol(next->d_name, NULL, 0);  
  133.   
  134.             //printf("hello,%s\n",next->d_name);  
  135.   
  136.             return 1;  
  137.             }  
  138.         }  
  139.    
  140.         //if (pidList)   
  141.     //{  
  142.             //pidList[i]=0;  
  143.         //}  
  144.         return 0;  
  145. }  
  146.   
  147. int main(int argc,char *argv[])  
  148. {  
  149.     int fd = 0;  
  150.     char buf[100];  
  151.   
  152.     //开启守护进程  
  153.     daemon(0,0);  
  154.   
  155.     while (1)  
  156.     {  
  157.         //打开日志  
  158.         //openlog(argv[0],LOG_CONS|LOG_PID,LOG_USER);  
  159.           
  160.         //判断是否有程序文件运行  
  161.         if (judge_pid_exist(NAME))  
  162.         {  
  163.             //syslog(LOG_INFO,"jdh success!!!!!!!!!!");  
  164.             //printf("hello,jdh,exist\n");    
  165.         }  
  166.         else  
  167.         {  
  168.             //syslog(LOG_INFO,"jdh fail!!!!!!!!!!");  
  169.             //运行程序  
  170.             system(RUN_NAME);  
  171.             //printf("hello,jdh,oh,no\n");  
  172.         }  
  173.           
  174.         //休眠  
  175.         sleep(60);  
  176.     }  
  177.   
  178.     //关闭日志  
  179.     //closelog();  
  180.   
  181.     return 0;  
  182. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值