windows系統下的程序監測程式--實現過程記錄
單位的客服中心的伺服器有個介面程式,這三天都出現了自動退出的情況,這介面程式是管理打進來的電話分配到各值班的客服人員那裡,介面程式是由其他公司開發的,以前也出現過自動退出的情況,但開發方一直沒給出解決方案。
由於這介面一退出,那外面的使用者就打不進電話來,結果使用者就投訴到領導那裡,領導就罵下面的員工。。。(低人一級就得捱罵,哎)
最近三天頻繁自動退出,我就產生自己開發一個監測該介面程式的程式。當介面程式自動退出時,就重啟服務。本來用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\n"); exit(-1); } fprintf(recordFile,"%s %s\n",getTime(),"begin to record"); if((listFile = fopen(listFileName,"r")) != NULL){ //if list file exist,del it fprintf(recordFile,"%s %s\n",getTime(),"delete old dsir.txt"); fclose(listFile); if(remove(listFileName)){ fprintf(recordFile,"%s %s\n",getTime(),"remove list file fail\n"); exit(-1); } } if(listFile != NULL){ printf("close list file\n"); fclose(listFile); } if(recordFile != NULL){ printf("close record file\n"); 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\n",lineCount); if(lineCount == 0){ // start one process fprintf(recordFile,"%s %s\n",getTime(),"start this process\n"); system(toExeProcess); }else if(lineCount == 1){ //ok status fprintf(recordFile,"%s %s\n",getTime(),"single process running\n"); }else if(lineCount > 1){ //too many processes, kill all and start one fprintf(recordFile,"%s %s\n",getTime(),"too many processes running\n"); system(killProcessCmd); Sleep(1000L); system(toExeProcess); } lineCount = 0; Sleep(sleepSeconds); } if(listFile != NULL){ printf("close list file\n"); fclose(listFile); } } int openFile(FILE *fp, char *str, char *mode){ fp = fopen(str,mode); printf("%s status:%d\n",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 == '\n'){ h++; }else{ c = fgetc(fp); if(c == EOF){ h++; break; } } } fclose(fp); return h; }