Reposted by the Fire Station Building Institute (bkjia.com)No matter what program can be perfect, theoretically, any program has a Core Dump day, the formal operation of the program, especially the server program, once the Core Dump, the consequences are unimaginable, if you have experience in server development, you must have had a late-night dream. You have been awakened by the phone call, restarted the server in a hurry, and your boss will be given a great deal of approval when you go to work the next day. Therefore, it is important to automatically restart a program when an error occurs. Here we will focus on how to automatically restart a program in linux.
There are many methods to implement automatic restart of programs in linux. Here we will introduce the implementation through writing scripts by ourselves.
The Automatic Restart script assumes that the program to be restarted is named test. Here, we determine whether the program is normal by judging the number of processes.
Ps-ef | grep "$1" | grep-v "grep" | wc-l is the number of processes that get $1 (test in this example, the script determines the next operation based on the number of processes. Through an infinite loop, the number of processes of the specified program in the system is checked every one second.
The Code is as follows:
Reference content is as follows: Script check #! /Bin/sh
#------------------------------------------------------------------------------ # Function: CheckProcess # Function: Check whether a process exists # Parameter: $1 --- Name of the process to be checked # Return: If 0 exists, otherwise 1 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 "CheckQQ_RET" If [$ CheckQQ_RET! = 4]; Then # Kill all the test processes and change the operations you want to perform. Killall-9 test Exec./test & Fi Sleep 1 Done |
Script start:
Add limit coredumpsize 102400 to set the core file size. Once the program Core Dump, traces can be found. Running the check script in the background saves a lot of trouble,