Linux to automatically restart the program with a script _unix Linux

Source: Internet
Author: User

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

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.