詳解Linux監控重要程序的實現方法

2022-10-06 15:48:17 字數 3236 閱讀 3554

不管後台服務程式寫的多麼健壯,還是可能會出現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() }

# check process number

# $1 : process name

function check_process()

# 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

#include

#include

#include

#include

#include

#include

#include

#define log_file "/var/log/supervisor.log"

void s_log(char *text)

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 **ar**) ;

pid_t pid;

if (argc < 2)

for (i = 1; i < argc; ++i)

while(1)

if (pid == 0)

s_log("exit child process");

exit(0);

} if (pid > 0)

} return 0;

} (4) 測試驗證

a. 假設需要自動重啟的程式為demo.c,其**實現如下所示:

/* * * demo

* */

#include

#include

#include

#include

#include

#include

#include

#include

#define log_file "/var/log/demo.log"

void demo_log(int num)

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 **ar**)

} 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 程序監控

1 ps命令 直接在linux系統中輸入 ps 結果如下 預設情況下,ps命令指揮顯示執行在當前控制台下的屬於當前使用者的程序。pid 程式的程序號 tty 程式執行的終端 time 程式執行的時間 引數 在linux系統中,程序的狀態有五種 1.執行 正在執行或在執行佇列中等待 2.中斷 休眠中,...

linux 程序監控

supervise是daemontools的乙個工具,可以用來監控管理unix下的應用程式運 況,在應用程式出現異常時,supervise可以重新啟動指定程式。使用 mkdir test cd test vim run 寫入希望執行的操作 supervise test 注意這裡是的引數是run檔案上...

linux 程序監控

supervise是daemontools的乙個工具,可以用來監控管理unix下的應用程式運 況,在應用程式出現異常時,supervise可以重新啟動指定程式。使用 mkdir test cd test vim run 寫入希望執行的操作 supervise test 注意這裡是的引數是run檔案上...