Windowsシステムの下のプロセスモニタリングプログラム--プロセス記録を実現する


ユニットのカスタマーサービスセンターのサーバーにはインタフェースプログラムがあり、この3日間は自動的に退出する状況が発生した.このインタフェースプログラムは管理から入ってきた電話が各当直のカスタマーサービス員に割り当てられている.インタフェースプログラムは他の会社が開発したもので、以前も自動的に退出したことがあるが、開発側は解決策を提供していない.
このインタフェースが終了すると、外のユーザーから電話がかかってこないので、ユーザーはリーダーに苦情を言い、リーダーは下の従業員をののしった.の(低い人は1級で叱られなければならない.
 
最近3日間頻繁に自動的に終了して、私は自分でこのインタフェースプログラムを監視するプログラムを開発しました.インタフェースプログラムが自動的に終了すると、サービスが再起動されます.もともとjavaを使うと、簡単だと思いますが、サーバーにはjdkがインストールされていません.一般的にサーバーはむやみにプログラムをインストールすることはできません.私はjavaで実現する考えを打ち消しました.後で考えてみると、その実用linuxのコマンドも簡単で、shellスクリプトを書いて、数行のコマンドを書けばいいのですが、サーバーはwindows serverシステムを使って、コップを飲みました.
 
元のc言語しか使えません.
最初、c言語で最初にしたことは、googleがc言語版のモニタリングプロセスプログラムがあるかどうか、もちろんありません.天下にこんないいことがあるわけがありません.持ってきてください.の
考え方を変えて、プロセスを監視するには、プロセスマネージャのように現在のプロセスリストを取得します.Googleを探してみます:c言語取得システムプロセス
結果も出ないし..
もう一度考えてjavaを使えばRuntimeを通過します.exec()はdosコマンドを実行します.同じように、c言語でdosコマンドをどのように実行するか、方法を見つけました.
system(char* commandStr);この関数は使えます.
第2歩はどのようにシステムプロセスを取得しますか?これは簡単にgoogleでdosの中の:tasklistコマンドを使うことを知っています.
取得したら取得したプロセスリストを解析します.問題がまた来ました.system()関数呼び出しの戻り結果はIntで、実行結果の状態を表します.だから私は別の道を切り開いて、tasklistの結果をtxtファイルにリダイレクトして、更にc言語でファイルを読んで、もともとここで私は1本の命令でプロセスのあの一行を取得したいのですが、仕方なく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(&ltime); 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; }