Linux中用C++写的监控Java进程的程序代码:
#include<iostream>
#include<unistd.h>
#include<string.h>
using namespace std;
int main()
{
while(true)
{
system("ps auwx | grep \"java -jar BusinessServer.jar\" | grep -v \"grep\" > MonitorServer.log");
sleep(1);
FILE* pFile = fopen("/usr/myprogram/MonitorServer.log","r");
char result[512] = "\0";
if(pFile != NULL)
{
fscanf(pFile,"%s",result);
fclose(pFile);
}
if(strcmp(result,"\0") != 0)
{
cout<<"BusinessServer.jar is alive"<<endl;
}
else
{
system("java -jar BusinessServer.jar &");
cout<<"BusinessServer.jar is dead"<<endl;
}
}
return 0;
}