Linux Monitoring Program-Automatic Program Restart Method

Source: Internet
Author: User

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 -9 Test Exec ./ Test & Fi Sleep 1 done

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.