zoukankan      html  css  js  c++  java
  • windows系统下的进程监测程序--实现过程记录

    原文出处:http://bosshida.iteye.com/blog/1060503

    单位的客服中心的服务器有个接口程序,这三天都出现了自动退出的情况,这接口程序是管理打进来的电话分配到各值班的客服人员那里,接口程序是由其他公司开发的,以前也出现过自动退出的情况,但开发方一直没给出解决方案。

    由于这接口一退出,那外面的用户就打不进电话来,结果用户就投诉到领导那里,领导就骂下面的员工。。。(低人一级就得挨骂,哎)

     

    最近三天频繁自动退出,我就产生自己开发一个监测该接口程序的程序。当接口程序自动退出时,就重启服务。本来用java的话,我觉得挺简单的,但服务器上没装jdk,一般服务器不能乱装程序的,我就打消了用java实现的念头,后来一想,其实用linux的命令也很简单,写个shell脚本,几行命令就行了,但服务器是用windows server系统,杯了个具。

     

    只能用原始的c语言了。

    刚开始时,用c语言实现第一件做的事是:google一下有无c语言版的监测进程程序,当然没有啦,天下哪有这么好的事,拿来就可以用。。。

    就转变思路,要监测一个进程,那就要获取当前的进程列表,像进程管理器那样。再找google一下:c语言 获取系统进程

    也是没结果。。。

    再想一下,如果用java,我就会通过Runtime.exec()来执行dos命令,同理,用c语言怎样执行dos命令呢,找到方法了:

    system(char* commandStr);这函数可以用。

    第二步是怎样获取系统进程?这个简单一google就知道用dos里的:tasklist命令。

    获取了之后就要解析获取到的进程列表,问题又来了,system()函数调用的返回结果是Int,表示执行结果状态。所以我就另辟蹊径,把tasklist的结果重定向到txt文件,再用c语言读文件,本来这里我是想用一条命令就把进程那一行获取出来的,但无奈windows系统不支持管道,还是linux系统牛B啊。

    好了,在获取系统当前运行的进程后,就通过dos的一条命令:findstr(类似于linux的find),找出我监测的进程,

    再把结果存入一个临时文件里

    接下来的工作就是读文件,若文件是0行,则代表监测的进程没启动,这时就启动该进程,若文件有1行,就代表程序正在运行,就等待5分钟再检测。如果文件有大于1行,代表启动多个进程,这是不允许的情况,就先把所有的进程关闭,再开一个进程。

     

    程序代码如下:(程序以记事本为demo,notepad.exe)

    #include<stdio.h>  
    #include<time.h>  
    #include <#include<stdio.h>  
    #include<time.h>  
    #include <windows.h>  
      
    #define listFileName "C:\dsir.txt"  
    #define recordFileName "c:\record.txt"  
    #define tmpFileName "c:\tmpProcess.txt"  
    #define processName "notepad.exe"  
    #define maxCharOneLine 100  
    #define sleepSeconds (1000*8)  
    #define toExeProcess "notepad.exe"  
    #define processImageName "NOTEPAD.EXE"  
      
    int openFile(FILE*,char*,char*);  
    char* getTime();  
    int getLineCount(char*);  
      
    //算法:检测列表文件存在否,存在就删除;  
    //进入循环:获取当前进程列表,保存入列表文件;  
    //查找列表文件有否Dsir程序,若只有一个则记录文件写入正常,若没有则记录异常,并尝试重启服务   
    //若多于一个则记录多个运行,并尝试关闭所有,然后开启一个。暂停5分钟,继续循环   
    //sleep(); sprintf(); tasklist; system()  
    main(){  
        FILE *listFile;  
        FILE *recordFile;  
        char getProcessesCmd[50];  
        char findDsirCmd[100];  
        char* line;  
        int lineCount = 0;  
          
        if((recordFile = fopen(recordFileName,"a+")) == NULL){ //record file not exist  
            printf("fail to open record file
    ");  
            exit(-1);  
        }  
        fprintf(recordFile,"%s %s
    ",getTime(),"begin to record");  
        if((listFile = fopen(listFileName,"r")) != NULL){ //if list file exist,del it  
            fprintf(recordFile,"%s %s
    ",getTime(),"delete old dsir.txt");  
            fclose(listFile);  
            if(remove(listFileName)){   
                fprintf(recordFile,"%s %s
    ",getTime(),"remove list file fail
    ");  
                exit(-1);  
            }  
        }  
          
        if(listFile != NULL){  
            printf("close list file
    ");  
            fclose(listFile);  
        }  
        if(recordFile != NULL){  
            printf("close record file
    ");  
            fclose(recordFile);  
        }  
          
        char killProcessCmd[100];  
        sprintf(getProcessesCmd, "tasklist /nh > %s", listFileName);  
        sprintf(killProcessCmd, "taskkill /im %s", processImageName);  
        sprintf(findDsirCmd, "findstr /c:%s %s > %s",processName,listFileName,tmpFileName);  
          
        while(1){  
            //get and save current processes into listFile  
            system(getProcessesCmd);  
            Sleep(1000L);  
            system(findDsirCmd);  
              
            lineCount = getLineCount(tmpFileName);  
            printf("lineCount: %d
    ",lineCount);  
              
            if(lineCount == 0){ // start one process  
                fprintf(recordFile,"%s %s
    ",getTime(),"start this process
    ");  
                system(toExeProcess);  
            }else if(lineCount == 1){ //ok status  
                fprintf(recordFile,"%s %s
    ",getTime(),"single process running
    ");  
            }else if(lineCount > 1){ //too many processes, kill all and start one  
                fprintf(recordFile,"%s %s
    ",getTime(),"too many processes running
    ");  
                system(killProcessCmd);  
                Sleep(1000L);  
                system(toExeProcess);  
            }  
              
            lineCount = 0;  
              
            Sleep(sleepSeconds);  
        }  
          
        if(listFile != NULL){  
            printf("close list file
    ");  
            fclose(listFile);  
        }  
    }  
      
    int openFile(FILE *fp, char *str, char *mode){  
        fp = fopen(str,mode);  
        printf("%s status:%d
    ",str,fp);  
        return fp==NULL ? 0 : 1;  
    }  
      
    char timeStr[50];  
    char* getTime(){  
        struct tm *newtime;  
        long ltime;  
        ltime = time(NULL);  
        newtime = localtime(<ime);  
        sprintf(timeStr, "%4d年%2d月%2d日%2d时%2d分%2d秒",newtime->tm_year + 1900,newtime->tm_mon + 1,newtime->tm_mday,  
        newtime->tm_hour, newtime->tm_min, newtime->tm_sec);  
        return timeStr;  
    }  
      
    int getLineCount(char* fName){  
        char c;  
        int h = 0;  
        FILE *fp = fopen(fName,"r");  
        if(fp == NULL){  
            return -1;  
        }  
        while((c = fgetc(fp)) != EOF){  
            if(c == '
    '){  
                h++;  
            }else{  
                c = fgetc(fp);  
                if(c == EOF){  
                    h++;  
                    break;  
                }  
            }  
        }  
        fclose(fp);  
        return h;  
    }  



  • 相关阅读:
    x-pack-crack
    ELK获取用户真实IP
    多层代理获取用户真实IP
    ELK帮助文档
    logstash filter plugin
    开源实时日志分析ELK平台部署
    消息队列集群kafka安装配置
    日志采集客户端 filebeat 安装部署
    rsync + inotify 同步
    【转】OpenWrt 防火墙/etc/config/firewall介绍
  • 原文地址:https://www.cnblogs.com/CCCrunner/p/11781806.html
Copyright © 2011-2022 走看看