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
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;
}
(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);
}
}
b. 測試準備和說明:
b1. 以上相關服務程式編譯後的二進制檔案為: supervisor 和 demo
b2. 執行如下測試指令 ./supervisor ./demo
c. 測試的結果:
c1. execv(progname, arg) 執行成功後,其後的代碼不會執行;隻有當執行錯誤時,才會傳回 -1。原來調用execv程序的代碼段會被progname應用程式的代碼段替換。
c2. 當kill掉子程序時,父程序wait函數會接收到子程序退出的信号,進而循環再啟動子程序,此過程實時性非常高。
c3. 當kill掉父程序時,子程序會被init程序接管,如果此時再kill掉子程序,則子程序會退出。
c4. 當同時kill掉父子程序,則父子程序都會退出。