国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

Linux|Centos|Ubuntu|系統(tǒng)進(jìn)程|Fedora|注冊表|Bios|Solaris|Windows7|Windows10|Windows11|windows server|

服務(wù)器之家 - 服務(wù)器系統(tǒng) - Linux - 詳解Linux監(jiān)控重要進(jìn)程的實(shí)現(xiàn)方法

詳解Linux監(jiān)控重要進(jìn)程的實(shí)現(xiàn)方法

2022-03-08 17:32lcy_ltpsr Linux

這篇文章主要介紹了詳解Linux監(jiān)控重要進(jìn)程的實(shí)現(xiàn)方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

不管后臺服務(wù)程序?qū)懙亩嗝唇眩€是可能會出現(xiàn)core dump等程序異常退出的情況,但是一般情況下需要在無

人為干預(yù)情況下,能夠自動重新啟動,保證服務(wù)進(jìn)程能夠服務(wù)用戶。這時就需要一個監(jiān)控程序來實(shí)現(xiàn)能夠讓服務(wù)進(jìn)程自動重新啟動。查閱相關(guān)資料及嘗試一些方法之后,總結(jié)linux系統(tǒng)監(jiān)控重要進(jìn)程的實(shí)現(xiàn)方法:腳本檢測和子進(jìn)程替換。

1、腳本檢測

(1) 基本思路: 通過shell命令(ps -e | grep "$1" | grep -v "grep" | wc -l) 獲取 $1 ($1 代表進(jìn)程的名字)的進(jìn)程數(shù),腳本根據(jù)進(jìn)程數(shù)來決定下一步的操作。通過一個死循環(huán),每隔幾秒檢查一次系統(tǒng)中的指定程序的進(jìn)程數(shù),這里也可使用crontab來實(shí)現(xiàn)。

(2) 具體實(shí)現(xiàn)過程的代碼如下: [ supervisor.sh ]

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#! /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
done

2、子進(jìn)程替換

(1) 基本思路:

a. 使用fork函數(shù)創(chuàng)建一個新的進(jìn)程,在進(jìn)程表中創(chuàng)建一個新的表項(xiàng),而創(chuàng)建者(即父進(jìn)程)按原來的流程繼續(xù)執(zhí)行,子進(jìn)程執(zhí)行自己的控制流程

b. 運(yùn)用execv函數(shù)把當(dāng)前進(jìn)程替換為一個新的進(jìn)程,新進(jìn)程由path或file參數(shù)指定,可以使用execv函數(shù)將程序的執(zhí)行從一個程序切換到另一個程序

c. 當(dāng)fork啟動一個子進(jìn)程時,子進(jìn)程就有了它自己的生命周期并將獨(dú)立運(yùn)行,此時可以在父進(jìn)程中調(diào)用wait函數(shù)讓父進(jìn)程等待子進(jìn)程的結(jié)束

(2) 基本的實(shí)現(xiàn)步驟:

a. 首先使用fork系統(tǒng)調(diào)用,創(chuàng)建子進(jìn)程

b. 在子進(jìn)程中使用execv函數(shù),執(zhí)行需要自動重啟的程序

c. 在父進(jìn)程中執(zhí)行wait函數(shù)等待子進(jìn)程的結(jié)束,然后重新創(chuàng)建一個新的子進(jìn)程

(3) 具體實(shí)現(xiàn)的代碼如下: supervisor.c

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/**
 *
 * 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) 測試驗(yàn)證

a. 假設(shè)需要自動重啟的程序?yàn)閐emo.c,其代碼實(shí)現(xiàn)如下所示:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*
*
* 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. 測試準(zhǔn)備和說明:

b1. 以上相關(guān)服務(wù)程序編譯后的二進(jìn)制文件為: supervisor 和 demo

b2. 執(zhí)行如下測試命令 ./supervisor ./demo 

c. 測試的結(jié)果:

c1. execv(progname, arg) 執(zhí)行成功后,其后的代碼不會執(zhí)行;只有當(dāng)執(zhí)行錯誤時,才會返回 -1。原來調(diào)用execv進(jìn)程的代碼段會被progname應(yīng)用程序的代碼段替換。

c2. 當(dāng)kill掉子進(jìn)程時,父進(jìn)程wait函數(shù)會接收到子進(jìn)程退出的信號,進(jìn)而循環(huán)再啟動子進(jìn)程,此過程實(shí)時性非常高。

c3. 當(dāng)kill掉父進(jìn)程時,子進(jìn)程會被init進(jìn)程接管,如果此時再kill掉子進(jìn)程,則子進(jìn)程會退出。

c4. 當(dāng)同時kill掉父子進(jìn)程,則父子進(jìn)程都會退出。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:https://blog.csdn.net/lcy4599/article/details/52267517

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 欧美一区永久视频免费观看 | a国产在线| 天堂精品一区二区三区 | 日韩综合一区 | 国产精品久久久久久久午夜 | 中文字幕人成乱码在线观看 | 偷拍呻吟高潮91 | 亚洲欧洲视频在线 | 亚洲福利一区二区 | 久久中文字幕在线观看 | 黄色毛片a | 色婷婷中文字幕 | 91社区在线观看 | 日韩精品一区二区三区在线播放 | 日韩免费av| 激情综合网站 | 午夜精品| 免费国产视频 | 精品久久久中文字幕 | 亚洲高清视频在线 | 一区二区三区国产 | 精品在线一区二区 | 欧美在线a| 欧美国产日韩精品 | 亚洲高清色综合 | 成人在线播放网站 | 久久精品无码一区二区三区 | 亚洲精品在线观看网站 | 天堂视频在线 | 国产精品久久九九 | 成人国产精品久久 | 欧美午夜三级视频 | 久久精品国产一区二区三区 | 日韩中文在线 | a级在线| 欧美中文在线 | 精品国产不卡一区二区三区 | 国产精品久久av | 中文字幕av在线播放 | 免费观看一级特黄欧美大片 | 国产电影一区二区 |