用c实现进程监控,如果某一进程退出则重启
来源:互联网 发布:python中的生成器 编辑:程序博客网 时间:2024/06/05 15:39
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <linux/types.h>
#include <sys/wait.h>
#include <errno.h>
#include <unistd.h>
//要监控的程序
#define P1 "hello1"
#define P2 "hello2"
#define P3 "hello3"
#define PATH "."
/*****************************/
int is_run(char *name);
void killall_hello(void);
/*****************************/
//运行返回1,没有运行返回0
int is_run(char *name)
{
int ret = -1;
char buf[256] = {'\0'};
snprintf(buf, sizeof(buf), "pidof %s >> /dev/null", name);
ret = system(buf);
if(!WEXITSTATUS(ret)){ //#include <sys/wait.h>
//printf("%s is running!\n", name);
return 1;
}
else{
//printf("%s is not running!\n", name);
return 0;
}
}
void killall_hello(void)
{
char buf[128] = {'\0'};
snprintf(buf, sizeof(buf), "killall %s", P1);
system(buf);
snprintf(buf, sizeof(buf), "killall %s", P2);
system(buf);
snprintf(buf, sizeof(buf), "killall %s", P3);
system(buf);
}
int main(int argc, char **argv)
{
int p1_ret = -1;
int p2_ret = -1;
int p3_ret = -1;
char buf[128];
bzero(buf, sizeof(buf));
while(1){
p1_ret = is_run(P1);
p2_ret = is_run(P2);
p3_ret = is_run(P3);
if (!(p1_ret && p2_ret && p3_ret))
{
killall_hello();
printf("rerun hello3\n");
snprintf(buf, sizeof(buf), "%s/%s &", PATH, P3);
system(buf);
//打印日志信息
system("date >> w.log");
if(!p1_ret){
snprintf(buf, sizeof(buf), "echo %s not run , killall , run %s >> w.log", P1, P3);
system(buf);
}
else if(!p2_ret){
snprintf(buf, sizeof(buf), "echo %s not run , killall , run %s >> w.log", P2, P3);
system(buf);
}
else if(!p3_ret){
snprintf(buf, sizeof(buf), "echo %s not run , killall , run %s >> w.log", P3, P3);
system(buf);
}
}
sleep(5);
}
exit(0);
}
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <linux/types.h>
#include <sys/wait.h>
#include <errno.h>
#include <unistd.h>
//要监控的程序
#define P1 "hello1"
#define P2 "hello2"
#define P3 "hello3"
#define PATH "."
/*****************************/
int is_run(char *name);
void killall_hello(void);
/*****************************/
//运行返回1,没有运行返回0
int is_run(char *name)
{
int ret = -1;
char buf[256] = {'\0'};
snprintf(buf, sizeof(buf), "pidof %s >> /dev/null", name);
ret = system(buf);
if(!WEXITSTATUS(ret)){ //#include <sys/wait.h>
//printf("%s is running!\n", name);
return 1;
}
else{
//printf("%s is not running!\n", name);
return 0;
}
}
void killall_hello(void)
{
char buf[128] = {'\0'};
snprintf(buf, sizeof(buf), "killall %s", P1);
system(buf);
snprintf(buf, sizeof(buf), "killall %s", P2);
system(buf);
snprintf(buf, sizeof(buf), "killall %s", P3);
system(buf);
}
int main(int argc, char **argv)
{
int p1_ret = -1;
int p2_ret = -1;
int p3_ret = -1;
char buf[128];
bzero(buf, sizeof(buf));
while(1){
p1_ret = is_run(P1);
p2_ret = is_run(P2);
p3_ret = is_run(P3);
if (!(p1_ret && p2_ret && p3_ret))
{
killall_hello();
printf("rerun hello3\n");
snprintf(buf, sizeof(buf), "%s/%s &", PATH, P3);
system(buf);
//打印日志信息
system("date >> w.log");
if(!p1_ret){
snprintf(buf, sizeof(buf), "echo %s not run , killall , run %s >> w.log", P1, P3);
system(buf);
}
else if(!p2_ret){
snprintf(buf, sizeof(buf), "echo %s not run , killall , run %s >> w.log", P2, P3);
system(buf);
}
else if(!p3_ret){
snprintf(buf, sizeof(buf), "echo %s not run , killall , run %s >> w.log", P3, P3);
system(buf);
}
}
sleep(5);
}
exit(0);
}
- 用c实现进程监控,如果某一进程退出则重启
- 用c实现进程监控,如果某一进程退出则重启
- linux shell脚本监控进程,如果进程不在,则重启之
- shell脚本实现某一进程是否运行结束的监控
- 用iopp.c监控进程I/O
- python 进程 Ctrl+C退出
- Linux下C/C++查找某一进程
- Linux下C/C++查找某一进程
- Linux下C/C++查找某一进程
- 进程退出
- 进程退出
- 进程退出
- 进程退出
- 进程退出
- 进程退出
- [Windows 监控]使用windows自带的工具去对某一进程实现监控,并取得这一进程对系统的资源使用情况
- Windows-进程-退出进程
- 【C语言】【unix c】进程的退出
- 通过代码学习C#&.NET——委托使用(通用类设计)
- RQNOJ 34
- Delphi设计带图标的菜单.6
- ubunt 下安装vim
- 设置yum工具
- 用c实现进程监控,如果某一进程退出则重启
- 随想、、、
- android中MediaPlayer和SoundPool的声音管理
- strlen 与 sizeof
- Unity3D 游戏引擎之实现平面多点触摸(二)
- java内部类、静态内部类 小结
- Emacs才是世界上最强的IDE
- [荐]memcpy、memmove、strcpy的区别?
- flex中的数据绑定 bindable