No matter what the program is not perfect, theoretically, any program has a core dump of the day, the formal operation of the program, especially the server program, once the core dump, disastrous consequences, have the experience of server development friends, must have experienced a late night dream, was awakened by the painful experience of the phone, Rush to restart the server, the next day to go to work by the boss a tough batch. Therefore, automatic restart becomes important when an error occurs in a program. Here we focus on the Linux implementation method of automatic restart program.
Linux implementation of the automatic restart of the program there are many ways, here we are introduced by their own script to achieve,
Automatic restart Script
Assuming that you need to implement a restart program named Test, we can determine whether the program is normal by judging the number of processes.
Ps-ef | grep "$" | Grep-v "grep" | Wc–l is to get the number of processes in this case, test, which determines the next action based on the number of processes. Through a dead loop, check the number of processes in the system for the specified program every 1 seconds.
The code is as follows:
Script Check
#!/bin/sh
#-----------------------------------# function: checkprocess# function: Check if a process exists # parameter:---The process name to check returns: Returns 1.# if there is a return of 0-------- -------------------------------checkprocess () {# Checks whether the input parameters are valid if [' $ ' = ']; Then return 1 fi # $PROCESS _num Gets the number of specified process names, returns 0 for 1, indicates normal, does not 1 returns 1, indicates an error and needs to be restarted process_num= ' Ps-ef | grep "$" | Grep-v "grep" | Wc-l ' If [$PROCESS _num-eq 1]; Then return 0 else return 1 fi}
# Check if the test instance already exists while [1]; Do checkprocess "Test" checkqq_ret=$? If [$CheckQQ _ret-eq 1]; then# kills all test processes and can change any action you need to perform killall-9 test exec./test & fi Sleep 1done
Script Start:
Add Limit coredumpsize 102400, set the size of the core file, once the program core Dump, there are traces to be found. In this script, you can save a lot of trouble by executing a check script in the background,
#!/bin/cshlimit coredumpsize 102400
./check