C++实现Windows下的Daemon,监控多个进程

转载于:https://blog.csdn.net/zhouzhou19880708/article/details/48266465

在windows下,可以用简单的bat实现守护进程的功能,如果dump掉就重新拉起来,百度下就能查到,举个例子:

  1. @echo off

  2.  
  3. ::检测时间间隔,单位:秒

  4. set _interval=5

  5.  
  6. ::需要守护的进程名称

  7. set _processName=print_hello.exe

  8.  
  9. ::需要守护的进程启动命令

  10. set _processCmd=D:\SVN\print_hello.exe

  11. ::set _processCmd=print_hello.exe

  12. ::需要守护的进程预估启动完毕所需时间,单位:秒

  13. set _processTimeout=1

  14.  
  15. ::进程用户名,一般是Administrator

  16. set _username=adminstrator

  17.  
  18. :LOOP

  19. set /a isAlive=false

  20. ::tasklist /FI "username eq %_username%" | find /C "%_processName%" > temp.txt

  21. tasklist | find /C "%_processName%" > temp.txt

  22. set /p num= < temp.txt

  23. del /F temp.txt

  24.  
  25. if "%num%" == "0" (

  26. start %_processCmd% | echo start %_processName% at %time%

  27. choice /D y /t %_processTimeout% > nul

  28. )

  29.  
  30. if "%num%" NEQ "0" echo %_processName% is running

  31. choice /D y /t %_interval% >nul

  32. goto LOOP

这段脚本就可以实现一个Daemon,隔5秒监控print_hello.exe程序一次,保证此程序在系统中的存活,可以用相对路径,也可以用绝对路径;

但用脚本总归有一些限制,比如我这边项目需要监控多个进程,而且是同名的进程,仅能用路径进行区分,对于这类的需求,以上的脚本实现起来相对麻烦,于是自己用C++开发了一个简单的Daemon.exe,通过读取配置文件,可同时监控多个进程,代码如下:

 
  1. #include <iostream>

  2. #include <windows.h>

  3. #include <process.h>

  4. #include <stdio.h>

  5. #include <tchar.h>

  6. #include <string.h>

  7. using namespace std;

  8.  
  9. #define MAX_THREAD 5

  10.  
  11. DWORD WINAPI Daemonproc(LPVOID lpParameter)

  12. {

  13. char *path = (char *)lpParameter;

  14. cout << "child process " << path<< endl;

  15. STARTUPINFO si;

  16.  
  17. PROCESS_INFORMATION pi;

  18.  
  19. ZeroMemory(&si, sizeof(si));

  20. si.cb = sizeof(si);

  21. ZeroMemory(&pi, sizeof(pi));

  22. do{

  23. if(!CreateProcess( NULL,path,NULL,NULL,FALSE,0,NULL,NULL,&si,&pi))

  24. {

  25. cout << "创建进程失败.." << GetLastError() << endl;

  26. return 0;

  27. }

  28.  
  29. WaitForSingleObject( pi.hProcess, INFINITE);

  30.  
  31. cout << "子进程已经退出..." << endl;

  32.  
  33. CloseHandle(pi.hProcess);

  34. CloseHandle(pi.hThread);

  35. }while(true);

  36.  
  37. return 0;

  38. }

  39.  
  40.  
  41. int main(int argc, char *argv[])

  42. {

  43. char number[32] = {0};

  44. char **path;

  45. char chpath[128] = {0};

  46. string strpath;

  47. GetModuleFileName(NULL,(LPSTR)chpath,sizeof(chpath));

  48. strpath = chpath;

  49.  
  50. size_t pos = strpath.rfind("\\");

  51. string strpath2 = strpath.substr(0, pos);

  52. strpath2 +="\\guard.ini";

  53.  
  54. GetPrivateProfileString("PROCESSNUM", "num", NULL, number, 32, strpath2.c_str());

  55.  
  56. int pnum = atoi(number);

  57. path = (char **)malloc(pnum * sizeof(char*));

  58. for (int i = 0; i < pnum; i++)

  59. {

  60. path[i] = (char*)malloc(128);

  61. }

  62. char c_tmp[32] = {0};

  63. HANDLE hd[MAX_THREAD];

  64. for (int i = 0; i < pnum; i++)

  65. {

  66. memset(c_tmp, 0, 32);

  67. memset(path[i], 0, 128);

  68. sprintf(c_tmp, "path%d", i+1);

  69. GetPrivateProfileString("PROCESSPATH", c_tmp, NULL, path[i], 128, strpath2.c_str());

  70. hd[i] = CreateThread(NULL,0,Daemonproc,path[i],0,NULL);

  71. if (hd[i] == NULL)

  72. {

  73. ExitProcess(i);

  74. }

  75.  
  76. }

  77. WaitForMultipleObjects(pnum,hd,TRUE,INFINITE);

  78. for ( int j = 0; j < pnum; j++)

  79. {

  80. CloseHandle(hd[j]);

  81. }

  82. }

猜你喜欢

转载自blog.csdn.net/pingfan2014/article/details/81362098