服務(wù)器
不管后臺服務(wù)程序?qū)懙亩嗝唇?,還是可能會出現(xiàn)core dump等程序異常退出的情況,但是一般情況下需要在無
人為干預(yù)情況下,能夠自動重新啟動,保證服務(wù)進程能夠服務(wù)用戶。這時就需要一個監(jiān)控程序來實現(xiàn)能夠讓服務(wù)進程自動重新啟動。查閱相關(guān)資料及嘗試一些方法之后,總結(jié)linux系統(tǒng)監(jiān)控重要進程的實現(xiàn)方法:腳本檢測和子進程替換。
1、腳本檢測
(1) 基本思路: 通過shell命令(ps -e | grep $1 | grep -v grep | wc -l) 獲取 $1 ($1 代表進程的名字)的進程數(shù),腳本根據(jù)進程數(shù)來決定下一步的操作。通過一個死循環(huán),每隔幾秒檢查一次系統(tǒng)中的指定程序的進程數(shù),這里也可使用crontab來實現(xiàn)。
(2) 具體實現(xiàn)過程的代碼如下: [ 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函數(shù)創(chuàng)建一個新的進程,在進程表中創(chuàng)建一個新的表項,而創(chuàng)建者(即父進程)按原來的流程繼續(xù)執(zhí)行,子進程執(zhí)行自己的控制流程
b. 運用execv函數(shù)把當前進程替換為一個新的進程,新進程由path或file參數(shù)指定,可以使用execv函數(shù)將程序的執(zhí)行從一個程序切換到另一個程序
c. 當fork啟動一個子進程時,子進程就有了它自己的生命周期并將獨立運行,此時可以在父進程中調(diào)用wait函數(shù)讓父進程等待子進程的結(jié)束
(2) 基本的實現(xiàn)步驟:
a. 首先使用fork系統(tǒng)調(diào)用,創(chuàng)建子進程
b. 在子進程中使用execv函數(shù),執(zhí)行需要自動重啟的程序
c. 在父進程中執(zhí)行wait函數(shù)等待子進程的結(jié)束,然后重新創(chuàng)建一個新的子進程
(3) 具體實現(xiàn)的代碼如下: 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. 假設(shè)需要自動重啟的程序為demo.c,其代碼實現(xiàn)如下所示:
/* * * 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 mess