实现思路
- 获取
ps命令输出并验证 - 读取
/proc文件系统内容并验证 / 参考
参考例程
- 程序一,检测某个服务是否运行,如果未运行则拉起。 ```c /**
- FileName :Monitor.c *
- Author : Tobiu *
- Description: 守护进程; *
- Date : Apr 18, 2018
**/
include
include
include
include
include
include
include
include
include
include
include
include
define MAXFILE 3
define USEPRINTF 1
/**
- @Function : program_Running
- @Description : 判断程序是否在运行
- @Param :
- service : 进程名
@return : exist : 1,no found value : 0 */ int program_Running(char service) { FILE fp; char cmd[128], buf[1024]; char *pLine;
sprintf(cmd, “/bin/ps aux | /bin/grep %s | /bin/grep -v grep”, service); fp = popen(cmd, “r”); while( fgets(buf, sizeof(buf), fp) != NULL ) {
pLine = strtok(buf, "\n");while( pLine ){if( strstr(pLine, service) ){pclose( fp );return 1;}pLine = strtok(NULL, "\n");}
} pclose( fp ); return 0; }
int main(int argc, char* argv[]) { if( USEPRINTF ) printf(“homecentor\n”);
pid_t pc, pid;pc = fork();if( pc < 0 ){if( USEPRINTF ) printf("error fork\n");exit(1);}else if( pc > 0 ){exit(0);}pid = setsid();if( pid < 0 )perror("setsid error");chdir("/");umask(0);int i;for(i=0; i<MAXFILE; i++)close(i);while( 1 ){/*监测homecentor*/if(!program_Running("homecenter")){/* The program did not run */system("/usr/bin/killall -9 homecenter"); //杀进程system("/bin/taskset -c 0 /home/mycode/bin/homecenter &"); //重新启动}sleep(2);}return 0;
开源工具
-
- iostat: 输出CPU的统计信息和所有I/O设备的输入输出(I/O)统计信息。
- mpstat: 关于CPU的详细信息(单独输出或者分组输出)。
- pidstat: 关于运行中的进程/任务、CPU、内存等的统计信息。
- sar: 保存并输出不同系统资源(CPU、内存、IO、网络、内核等。。。)的详细信息。
- sadc: 系统活动数据收集器,用于收集sar工具的后端数据。
- sa1: 系统收集并存储sadc数据文件的二进制数据,与sadc工具配合使用
- sa2: 配合sar工具使用,产生每日的摘要报告。
- sadf: 用于以不同的数据格式(CVS或者XML)来格式化sar工具的输出。
- Sysstat: sysstat工具的man帮助页面。
- nfsiostat: NFS(Network File System)的I/O统计信息。
- cifsiostat: CIFS(Common Internet File System)的统计信息。
参考文献
- Linux下sysstat安装使用图文详解
- 通过sysstat监控Linux各项参数
- Linux系统性能和使用活动监控工具 sysstat
- 使用vmstat和iostat命令进行Linux性能监控
- Zabbix 3.0 从入门到精通(zabbix使用详解)
