[Original] use shell scripts to monitor the process and automatically restart after the crash fails.

Source: Internet
Author: User

========================================================== ==========
This article is the original khler, and the original author information and the original link of this article must be kept completely and completely reprinted.

Author: heyuanhui
E-mail:Khler@163.com
QQ: 23381103
MSN:Pragmac@hotmail.com
========================================================== ==========

 

How can I ensure that the service is always running? How can I ensure automatic restart even if the service fails? This problem is often encountered when writing a service program. In Linux, a powerful shell can handle such transactions flexibly.

The following shell uses a while-do loop and uses PS-Ef | grep to check whether the loader process is running. If not, it starts, this ensures that the crashed process is restarted in time.

Note the following two points:

1. PS | grep must be added to a process; otherwise, grep may return incorrect results;

2. You must use-V to remove the grep command from the result. Otherwise, the result is not empty.

 

#! /Bin/sh
#====================================
# Yuanhui. He
# Khler@163.com
#====================================
While:
Do
Echo "Current DIR is" $ pwd
Stillrunning = $ (PS-Ef | grep "$ PWD/loader" | grep-V "grep ")
If ["$ stillrunning"]; then
Echo "TWS service was already started by another way"
Echo "kill it and then startup by this shell, other wise this shell will loop out this message annoyingly"
Kill-9 $ pidof $ PWD/loader
Else
Echo "TWS service was not started"
Echo "starting service ..."
$ PWD/loader
Echo "TWS service was exited! "
Fi
Sleep 10
Done

 

 

 

If a process already exists when the shell is started, it means that the process has been started in other ways, instead of the shell, it will continue to remind you to find the process. The solution is either to use this shell to start the service, you can either kill a service that is started in another way once it is found, and the preceding statement is as follows:

 

Kill-9 $ pidof $ PWD/loader

 

 

Related Article

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.