In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
No matter how robust the background server is written, it is still possible for programs such as core dump to exit abnormally, but in general, it is necessary to have no
In the case of human intervention, it can be automatically restarted to ensure that the service process can serve users. At this point, a monitor is needed to enable the service process to restart automatically. After consulting the relevant materials and trying some methods, this paper summarizes the implementation methods of linux system monitoring important processes: script detection and child process replacement.
1. Script detection
(1) basic idea: get the number of processes of $1 ($1 represents the name of the process) through the shell command (ps-e | grep "$1" | grep-v "grep" | wc-l), and the script determines the next step according to the number of processes. Through an endless loop, the number of processes of a specified program in the system is checked every few seconds, which can also be done using crontab.
(2) the code of the specific implementation process is as follows: [supervisor.sh]
#! / bin/sh # supervisor process LOG_FILE=/var/log/supervisor_sh.log # log function function log () {local tasking $(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 pendant numb $(ps-e | grep "$1" | grep-v "grep" | wc-l) log "pendant numb $p_num" echo $p_num} # supervisor process while [1] do declare-I ch_num num 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. Child process replacement
(1) basic ideas:
a. Use the fork function to create a new process and create a new table item in the process table, while the creator (that is, the parent process) continues to execute according to the original process, and the child process executes its own control flow.
b. Use the execv function to replace the current process with a new process specified by the path or file parameters. You can use the execv function to switch the execution of the program from one program to another.
c. When fork starts a child process, the child process has its own life cycle and will run independently. At this time, you can call the wait function in the parent process to wait for the end of the child process.
(2) basic implementation steps:
a. First create a child process using the fork system call
b. Use the execv function in a child process to execute a program that needs to be restarted automatically
c. Execute the wait function in the parent process to wait for the end of the child process, and then recreate a new child process
(3) the specific implementation code is as follows: supervisor.c
/ * supervisor * * date: 2016-08-10 * * / # include # 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; 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 ", 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) Test verification
a. Suppose the program that needs to be restarted automatically is demo.c, and its code implementation is as follows:
/ * demo * * / # include # 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; 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. Test preparation and description:
B1. The compiled binaries of the above related service programs are: supervisor and demo
B2. Execute the following test command. / supervisor. / demo
c. Results of the test:
C1. When execv (progname, arg) executes successfully, subsequent code does not execute;-1 is returned only if there is an error. The original code snippet that calls the execv process is replaced by the code snippet of the progname application.
C2. When the kill drops the child process, the wait function of the parent process will receive the signal of the exit of the child process, and then recycle and start the child process, which is very real-time.
C3. When kill drops the parent process, the child process will be taken over by the init process. If the child process is dropped again at this time, the child process will exit.
C4. When the parent and child processes are kill at the same time, both the parent and child processes will exit.
The above is the whole content of this article, I hope it will be helpful to your study, and I also hope that you will support it.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.