Linux监控重要进程的实现方法
Linux监控重要进程的实现方法
不管后台服务程序写的多么健壮,还是可能会出现core dump等程序异常退出的情况,但是一般情况下需要在无
人为干预情况下,能够自动重新启动,保证服务进程能够服务用户。这时就需要一个监控程序来实现能够让服务进程自动重新启动。查阅相关资料及尝试一些方法之后,总结linux系统监控重要进程的实现方法:脚本检测和子进程替换。
1、脚本检测 (1) 基本思路: 通过shell命令(ps -e | grep "$1" | grep -v "grep" | wc -l) 获取 $1 ($1 代表进程的名字)的进程数,脚本根据进程数来决定下一步的操作。通过一个死循环,每隔几秒检查一次系统中的指定程序的进程数,这里也可使用crontab来实现。 (2) 具体实现过程的代码如下: [ supervisor.sh ]
- #! /bin/sh
- # supervisor process
- LOG_FILE=/var/log/supervisor_sh.log
- # log function
- function log() {
- local t=$(date +"%F %X")
- echo "[ $t ] $0 : $1 " >> ${LOG_FILE}
- }
- # check process number
- # $1 : process name
- function check_process() {
- if [ -z $1 ]; then
- log "Input parameter is empty."
- return 0
- fi
- p_num=$(ps -e | grep "$1" | grep -v "grep" | wc -l)
- log "p_num = $p_num"
- echo $p_num
- }
- # supervisor process
- while [ 1 ]
- do
- declare -i ch_num
- p_name="apache2"
- ch_num=$(check_process $p_name)
- if [ $ch_num -eq 0 ]; then
- killall $p_name
- service $p_name start
- fi
- sleep 3
- done
#! /bin/sh
# supervisor process LOG_FILE=/var/log/supervisor_sh.log # log function
function log() {
local t=$(date +"%F %X")
echo "[ $t ] $0 : $1 " >> ${LOG_FILE}
} # check process number
# $1 : process name
function check_process() {
if [ -z $1 ]; then
log "Input parameter is empty."
return 0
fi p_num=$(ps -e | grep "$1" | grep -v "grep" | wc -l)
log "p_num = $p_num"
echo $p_num
} # supervisor process
while [ 1 ]
do
declare -i ch_num
p_name="apache2"
ch_num=$(check_process $p_name)
if [ $ch_num -eq 0 ]; then
killall $p_name
service $p_name start
fi
sleep 3
done
2、子进程替换 (1) 基本思路: a. 使用fork函数创建一个新的进程,在进程表中创建一个新的表项,而创建者(即父进程)按原来的流程继续执行,子进程执行自己的控制流程 b. 运用execv函数把当前进程替换为一个新的进程,新进程由path或file参数指定,可以使用execv函数将程序的执行从一个程序切换到另一个程序 c. 当fork启动一个子进程时,子进程就有了它自己的生命周期并将独立运行,此时可以在父进程中调用wait函数让父进程等待子进程的结束 (2) 基本的实现步骤: a. 首先使用fork系统调用,创建子进程 b. 在子进程中使用execv函数,执行需要自动重启的程序 c. 在父进程中执行wait函数等待子进程的结束,然后重新创建一个新的子进程 (3) 具体实现的代码如下: supervisor.c
- /**
- *
- * supervisor
- *
- * date: 2016-08-10
- *
- */
- #include <stdio.h>
- #include <unistd.h>
- #include <errno.h>
- #include <string.h>
- #include <sys/types.h>
- #include <sys/wait.h>
- #include <stdlib.h>
- #include <time.h>
- #define LOG_FILE "/var/log/supervisor.log"
- void s_log(char *text) {
- time_t t;
- struct tm *tm;
- char *log_file;
- FILE *fp_log;
- char date[128];
- log_file = LOG_FILE;
- fp_log = fopen(log_file, "a+");
- if (NULL == fp_log) {
- fprintf(stderr, "Could not open logfile '%s' for writing\n", log_file);
- }
- time(&t);
- tm = localtime(&t);
- strftime(date, 127, "%Y-%m-%d %H:%M:%S", tm);
- /* write the message to stdout and/or logfile */
- fprintf(fp_log, "[%s] %s\n", date, text);
- fflush(fp_log);
- fclose(fp_log);
- }
- int main(int argc, char **argv) {
- int ret, i, status;
- char *child_argv[100] = {0};
- pid_t pid;
- if (argc < 2) {
- fprintf(stderr, "Usage:%s <exe_path> <args...>", argv[0]);
- return -1;
- }
- for (i = 1; i < argc; ++i) {
- child_argv[i-1] = (char *)malloc(strlen(argv[i])+1);
- strncpy(child_argv[i-1], argv[i], strlen(argv[i]));
- //child_argv[i-1][strlen(argv[i])] = '0';
- }
- while(1) {
- pid = fork();
- if (pid == -1) {
- fprintf(stderr, "fork() error.errno:%d error:%s", errno, strerror(errno));
- break;
- }
- if (pid == 0) {
- s_log(child_argv[0]);
- ret = execv(child_argv[0], (char **)child_argv);
- s_log("execv return");
- if (ret < 0) {
- fprintf(stderr, "execv ret:%d errno:%d error:%s", ret, errno, strerror(errno));
- continue;
- }
- s_log("exit child process");
- exit(0);
- }
- if (pid > 0) {
- pid = wait(&status);
- fprintf(stdout, "Child process id: %d\n", pid);
- //fprintf(stdout, "wait return");
- s_log("Wait child process return");
- }
- }
- return 0;
- }
/**
*
* supervisor
*
* date: 2016-08-10
*
*/ #include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <time.h> #define LOG_FILE "/var/log/supervisor.log" void s_log(char *text) {
time_t t;
struct tm *tm;
char *log_file;
FILE *fp_log;
char date[128]; log_file = LOG_FILE;
fp_log = fopen(log_file, "a+");
if (NULL == fp_log) {
fprintf(stderr, "Could not open logfile '%s' for writing\n", log_file);
} time(&t);
tm = localtime(&t);
strftime(date, 127, "%Y-%m-%d %H:%M:%S", tm); /* write the message to stdout and/or logfile */
fprintf(fp_log, "[%s] %s\n", date, text);
fflush(fp_log);
fclose(fp_log);
} int main(int argc, char **argv) {
int ret, i, status;
char *child_argv[100] = {0};
pid_t pid;
if (argc < 2) {
fprintf(stderr, "Usage:%s <exe_path> <args...>", argv[0]);
return -1;
} for (i = 1; i < argc; ++i) {
child_argv[i-1] = (char *)malloc(strlen(argv[i])+1);
strncpy(child_argv[i-1], argv[i], strlen(argv[i]));
//child_argv[i-1][strlen(argv[i])] = '0';
} while(1) {
pid = fork();
if (pid == -1) {
fprintf(stderr, "fork() error.errno:%d error:%s", errno, strerror(errno));
break;
}
if (pid == 0) {
s_log(child_argv[0]);
ret = execv(child_argv[0], (char **)child_argv);
s_log("execv return");
if (ret < 0) {
fprintf(stderr, "execv ret:%d errno:%d error:%s", ret, errno, strerror(errno));
continue;
}
s_log("exit child process");
exit(0);
}
if (pid > 0) {
pid = wait(&status);
fprintf(stdout, "Child process id: %d\n", pid);
//fprintf(stdout, "wait return");
s_log("Wait child process return");
}
} return 0;
}
(4) 测试验证 a. 假设需要自动重启的程序为demo.c,其代码实现如下所示:
- /*
- *
- * demo
- *
- */
- #include <stdio.h>
- #include <unistd.h>
- #include <errno.h>
- #include <string.h>
- #include <sys/types.h>
- #include <sys/wait.h>
- #include <stdlib.h>
- #include <time.h>
- #define LOG_FILE "/var/log/demo.log"
- void demo_log(int num) {
- time_t t;
- struct tm *tm;
- char *log_file;
- FILE *fp_log;
- char date[128];
- log_file = LOG_FILE;
- fp_log = fopen(log_file, "a+");
- if (NULL == fp_log) {
- fprintf(stderr, "Could not open logfile '%s' for writing\n", log_file);
- }
- time(&t);
- tm = localtime(&t);
- strftime(date,127,"%Y-%m-%d %H:%M:%S",tm);
- /* write the message to stdout and/or logfile */
- fprintf(fp_log, "[%s] num = %d\n", date, num);
- fflush(fp_log);
- fclose(fp_log);
- }
- int main(int argc, char **argv[]) {
- int num = 0;
- while(1) {
- sleep(10);
- num++;
- demo_log(num);
- }
- }
/*
*
* demo
*
*/ #include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <time.h> #define LOG_FILE "/var/log/demo.log" void demo_log(int num) {
time_t t;
struct tm *tm;
char *log_file;
FILE *fp_log;
char date[128]; log_file = LOG_FILE;
fp_log = fopen(log_file, "a+");
if (NULL == fp_log) {
fprintf(stderr, "Could not open logfile '%s' for writing\n", log_file);
} time(&t);
tm = localtime(&t);
strftime(date,127,"%Y-%m-%d %H:%M:%S",tm); /* write the message to stdout and/or logfile */
fprintf(fp_log, "[%s] num = %d\n", date, num);
fflush(fp_log);
fclose(fp_log);
} int main(int argc, char **argv[]) {
int num = 0; while(1) {
sleep(10);
num++;
demo_log(num);
}
}
b. 测试准备和说明:
b1. 以上相关服务程序编译后的二进制文件为: supervisor 和 demo
b2. 执行如下测试命令 ./supervisor ./demo
c. 测试的结果:
c1. execv(progname, arg) 执行成功后,其后的代码不会执行;只有当执行错误时,才会返回 -1。原来调用execv进程的代码段会被progname应用程序的代码段替换。
c2. 当kill掉子进程时,父进程wait函数会接收到子进程退出的信号,进而循环再启动子进程,此过程实时性非常高。
c3. 当kill掉父进程时,子进程会被init进程接管,如果此时再kill掉子进程,则子进程会退出。
c4. 当同时kill掉父子进程,则父子进程都会退出。
Linux监控重要进程的实现方法的更多相关文章
- Linux/Unix分配进程ID的方法以及源代码实现
在Linux/Unix系统中.每一个进程都有一个非负整型表示的唯一进程ID.尽管是唯一的.可是进程的ID能够重用.当一个进程终止后,其进程ID就能够再次使用了. 大多数Linux/Unix系统採用延迟 ...
- 【Linux】- 守护进程的启动方法
转自:Linux 守护进程的启动方法 Linux中"守护进程"(daemon)就是一直在后台运行的进程(daemon). 本文介绍如何将一个 Web 应用,启动为守护进程. 一.问 ...
- Linux的僵尸进程及其解决方法
1. 产生原因: 在UNIX 系统中,一个进程结束了,但是他的父进程没有等待(调用wait / waitpid)他,那么他将变成一个僵尸进程.通过ps命令查看其带有defunct的标志.僵尸进程是一个 ...
- 利用VisualVm和JMX远程监控Java进程
自Java 6开始,Java程序启动时都会在JVM内部启动一个JMX agent,JMX agent会启动一个MBean server组件,把MBeans(Java平台标准的MBean + 你自己创建 ...
- linux 创建守护进程的相关知识
linux 创建守护进程的相关知识 http://www.114390.com/article/46410.htm linux 创建守护进程的相关知识,这篇文章主要介绍了linux 创建守护进程的相关 ...
- [转]❲阮一峰❳Linux 守护进程的启动方法
❲阮一峰❳Linux 守护进程的启动方法 "守护进程"(daemon)就是一直在后台运行的进程(daemon). 本文介绍如何将一个 Web 应用,启动为守护进程. 一.问题的由来 ...
- Linux中监控命令top命令使用方法详解
收集了两篇关于介绍Linux中监控命令top命令的详细使用方法的文章.总的来说,top命令主要用来查看Linux系统的各个进程和系统资源占用情况,在监控Linux系统性能方面top显得非常有用,下面就 ...
- 使用 shell 脚本对 Linux 系统和进程资源进行监控
Shell 简介 Shell 语言对于接触 LINUX 的人来说都比较熟悉,它是系统的用户界面,提供了用户与内核进行交互操作的一种接口.它接收用户输入的命令并把它送入内核去执行.实际上 Shell 是 ...
- Linux下,如何监控某个进程到底向哪个地址发起了网络调用
Linux下,如何监控某个进程到底向哪个地址发起了网络调用 有时候,有些应用,比如idea,你发起某个操作时,其底层会去请求网络,获取一些数据. 但是不知道,请求了什么地址.举个例子,在idea中,m ...
随机推荐
- 实训六(Cocos2dx游戏分享到微信朋友圈----AppID的获取)
考虑把游戏分享到微信朋友圈,前面的博文已经写到,shareSDK是一个很好的选择,但是学习了几天时间,遇到了很多问题,与其在一棵树上吊死,还不如退一步海阔天空,先暂时放一放,于是我考虑了一下既然是分享 ...
- Beta阶段冲刺-4
一. 每日会议 1. 照片 2. 昨日完成工作 3. 今日完成工作 4. 工作中遇到的困难 杨晨露:热......算不算困难......? 戴志斌:找了好几种框架,改了不少 游舒婷:不能相信开发工具自 ...
- Beta版本冲刺(五)
目录 组员情况 组员1(组长):胡绪佩 组员2:胡青元 组员3:庄卉 组员4:家灿 组员5:恺琳 组员6:翟丹丹 组员7:何家伟 组员8:政演 组员9:黄鸿杰 组员10:刘一好 组员11:何宇恒 展示 ...
- Linux基础六(网络管理)
目录 一.网络配置 1. IP 地址配置 2. 网络配置文件 3. 虚拟机网络配置参数 二.网络命令 1. 网络环境查看命令 2. 网络测试命令 三.远程会话安全协议-SSH原理 1. SSH 原理 ...
- SSM 关于service和dao的封装
近期由于客户需求,所以我们整个小组开始开发java项目. 虽然很久以前也是系统学习过.不过干了这么多年 .net ,有关java的早就扔了. 好了,废话不多说.我们看看SSM 关于service和d ...
- linux下转换windows文件格式为unix sed -i 's/\r//' <filename> 转化为unix格式
sed -i 's/\r//' <filename> 转化为unix格式
- 【刷题】LOJ 6121 「网络流 24 题」孤岛营救问题
题目描述 1944 年,特种兵麦克接到国防部的命令,要求立即赶赴太平洋上的一个孤岛,营救被敌军俘虏的大兵瑞恩.瑞恩被关押在一个迷宫里,迷宫地形复杂,但幸好麦克得到了迷宫的地形图.迷宫的外形是一个长方形 ...
- 20个令人惊叹的深度学习应用(Demo+Paper+Code)
20个令人惊叹的深度学习应用(Demo+Paper+Code) 从计算机视觉到自然语言处理,在过去的几年里,深度学习技术被应用到了数以百计的实际问题中.诸多案例也已经证明,深度学习能让工作比之前做得更 ...
- 【OpenCV】摄像机标定+畸变校正
摄像机标定 本文目的在于记录如何使用MATLAB做摄像机标定,并通过OpenCV进行校正后的显示. 首先关于校正的基本知识通过OpenCV官网的介绍即可简单了解: http://docs.open ...
- 洛谷 P4568 [JLOI2011]飞行路线 解题报告
P4568 [JLOI2011]飞行路线 题目描述 Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司.该航空公司一共在\(n\)个城市设有业务,设这些城市分别标记为0到\(n−1\ ...