From: http://www.cnblogs.com/zhy113/archive/2013/03/15/2960910.html
When writing data to a server, no matter how robust the server is, core dump and so on often occur.ProgramAbnormal exit, but generally it is necessary to automatically restart without human intervention to ensure that the server program can serve users. In this case, a monitoring program is required to enable the program to automatically restart. Now I have encountered this problem when writing Portmap, find a relatively reliable exec + fork solution.
Automatic Restart using scripts
The simplest way to use shell scripts is as follows:
PS-Ef | grep "$1" | grep-V "grep" | WC-L is the number of processes that get $1 ($1 indicates the name of the process, the script determines the next operation based on the number of processes. Through an endless loop, check the number of processes of the specified program in the system every one second. You can also use crontab here.
This method can solve the problem basically, but there is a latency of 1 s. I did not use this method in the application. For details about this shell script, seeArticleAttachmentCode.
Exec + fork Mode
The exec + fork method is adopted by the author. The specific ideas are as follows:
1. the exec function replaces the current process with a new process, which is specified by the path or file parameter. You can use the exec function to switch the execution of a program from one program to another;
2. The fork function creates a new process and creates a new table item in the table. The Creator (that is, the parent process) continues to execute the table according to the original process, sub-processes execute their own control processes;
3. When fork starts a sub-process, the sub-process has its own lifecycle and runs independently, we can call the wait function in the parent process to wait for the parent process to end with the child process;
I believe that the readers can come up with a solution here: 1) First, use the fork system to call and create a sub-process. 2) use the exec function in the sub-process, execute the program that needs to be automatically restarted. 3) execute wait in the parent process to wait for the completion of the Child process, and then create a new child process.
Usage:
#./Portmap the path of the program to be monitored # parameters required by ARGs Portmap$./Supervisor./Portmap ARGs .....
The Code is as follows:
/* * ** Supervisor ** Author: liyangguang (liyangguang@software.ict.ac.cn) ** Date: 21:04:01 ** changes * 1, execl to execv */ # Include <Stdio. h> # Include <Unistd. h># Include <Errno. h> # Include < String . H> # Include <Sys/types. h> # Include <Sys/Wait. H> # Include <Stdlib. h> 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...> <strong> n </strong> " , 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])] = ' <Strong> 0 </strong> ' ;} While ( 1 ){ PID = Fork (); If (Pid =- 1 ) {Fprintf (stderr, " Fork () error. errno: % d error: % S <strong> n </strong> " , Errno, strerror (errno )); } If (Pid = 0 ) {RET = Execv (child_argv [ 0 ], ( Char ** ) Child_argv ); If (Ret < 0 ) {Fprintf (stderr, " Execv RET: % d errno: % d error: % S <strong> n </strong> " , RET, errno, strerror (errno )); Continue ;} Exit ( 0 );} If (Pid> 0 ) {Pid = Wait (& Status ); Fprintf (stdout, " Wait return " );} } Return 0 ;}
The code for shell script is as follows:
# Function: checkprocess # function: Check whether a process exists # parameter: $1 --- Name of the process to be checked # Return: If yes, 0 is returned; otherwise, 1. # Success is returned .#------------------------------------------------------------------------------ Checkprocess (){ # Check whether the input parameters are valid If [ "$1" = "" ] ; Then Return 1 fi # $ Process_num: gets the number of specified process names. If the value is 1, 0 is returned. If the value is not 1, 1 is returned. If the value is not 1, an error is returned. Restart is required. Process_num =' PS -Ef |Grep "$1" | Grep -V "grep" | WC -L' if [ $ Process_num-EQ 1 ] ; Then Return 0 else Return 1 fi } # Check whether the test instance already exists While [ 1 ] ; Do checkprocess "test" checkqq_ret =$? If [ $ Checkqq_ret-EQ 1 ] ; Then # Kill all the test processes and change the operations you want to perform. Killall -9Test Exec ./ Test & Fi Sleep 1 done
From: http://www.cnblogs.com/zhy113/archive/2013/03/15/2960910.html
When writing data to the server, no matter how robust the server is, or the program such as core dump exits abnormally, it usually requires no human intervention, it can automatically restart to ensure that the server program can serve users. In this case, a monitoring program is required to enable the program to automatically restart. Now I have encountered this problem when writing Portmap, find a relatively reliable exec + fork solution.
Automatic Restart using scripts
The simplest way to use shell scripts is as follows:
PS-Ef | grep "$1" | grep-V "grep" | WC-L is the number of processes that get $1 ($1 indicates the name of the process, the script determines the next operation based on the number of processes. Through an endless loop, check the number of processes of the specified program in the system every one second. You can also use crontab here.
This method can solve the problem basically, but with a latency of 1 s, I did not use this method in the application. For this shell script, please refer to the attachment code after the article.
Exec + fork Mode
The exec + fork method is adopted by the author. The specific ideas are as follows:
1. the exec function replaces the current process with a new process, which is specified by the path or file parameter. You can use the exec function to switch the execution of a program from one program to another;
2. The fork function creates a new process and creates a new table item in the table. The Creator (that is, the parent process) continues to execute the table according to the original process, sub-processes execute their own control processes;
3. When fork starts a sub-process, the sub-process has its own lifecycle and runs independently, we can call the wait function in the parent process to wait for the parent process to end with the child process;
I believe that the readers can come up with a solution here: 1) First, use the fork system to call and create a sub-process. 2) use the exec function in the sub-process, execute the program that needs to be automatically restarted. 3) execute wait in the parent process to wait for the completion of the Child process, and then create a new child process.
Usage:
#./Portmap the path of the program to be monitored # parameters required by ARGs Portmap$./Supervisor./Portmap ARGs .....
The Code is as follows:
/* * ** Supervisor ** Author: liyangguang (liyangguang@software.ict.ac.cn) ** Date: 21:04:01 ** changes * 1, execl to execv */ # Include <Stdio. h># Include <Unistd. h> # Include <Errno. h> # Include < String . H> # Include <Sys/types. h> # Include <Sys/Wait. H> # Include <Stdlib. h> 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...> <strong> n </strong> " , 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])] = ' <Strong> 0 </strong> ' ;} While ( 1 ){ PID = Fork (); If (Pid =- 1 ) {Fprintf (stderr, " Fork () error. errno: % d error: % S <strong> n </strong> " , Errno, strerror (errno )); } If (Pid = 0 ) {RET = Execv (child_argv [ 0 ], ( Char ** ) Child_argv ); If (Ret < 0 ) {Fprintf (stderr, " Execv RET: % d errno: % d error: % S <strong> n </strong> " , RET, errno, strerror (errno )); Continue ;} Exit ( 0 );} If (Pid> 0 ) {Pid = Wait (& Status ); Fprintf (stdout, " Wait return " );} } Return 0 ;}
The code for shell script is as follows:
# Function: checkprocess # function: Check whether a process exists # parameter: $1 --- Name of the process to be checked # Return: If yes, 0 is returned; otherwise, 1. # Success is returned .#------------------------------------------------------------------------------ Checkprocess (){ # Check whether the input parameters are valid If [ "$1" = "" ] ; Then Return 1 fi# $ Process_num: gets the number of specified process names. If the value is 1, 0 is returned. If the value is not 1, 1 is returned. If the value is not 1, an error is returned. Restart is required. Process_num =' PS -Ef | Grep "$1" | Grep -V "grep" | WC -L' if [ $ Process_num-EQ 1 ] ; Then Return 0 else Return 1 fi } # Check whether the test instance already exists While [ 1 ] ; Do checkprocess "test" checkqq_ret =$? If [ $ Checkqq_ret-EQ 1 ] ; Then# Kill all the test processes and change the operations you want to perform. Killall -9 Test Exec ./ Test & Fi Sleep 1 done