Linux under monitoring process crashes automatically restart the shell script _linux shell after hanging off

Source: Internet
Author: User
=================================================
This article for Khler original, reprint must ensure that this article complete and completely retain the original author information and this article original link
Author:heyuanhui
E-mail:khler@163.com
qq:23381103
Msn:pragmac@hotmail.com
=================================================

How do I keep the service running? How to ensure that the service can be automatically restarted even if it is dead? This problem is often encountered when writing service programs. In a Linux system, a powerful shell can handle such transactions flexibly.
The following shell uses a while-do loop to check if the loader process is running and, if not, to start, so that the process of crashing the dead is restarted in time.
Two points must be noted:
1, PS |grep a process must add its road strength, otherwise easily grep to the wrong result;
2, must use-V to remove the grep command itself from the result, otherwise the result is not empty.
Copy Code code as follows:

#!/bin/sh
#=====================
#YuanHui. He
#khler @163.com
#=====================
While:
Todo
echo "Current DIR is" $PWD
stillrunning=$ (ps-ef |grep "$PWD/loader" |grep-v "grep")
If ["$stillRunning"]; Then
echo "TWS service is already started by another way"
echo "Kill it and then startup by the This shell, the other wise this shell'll loop out this message annoyingly"
Kill-9 $pidof $PWD/loader
Else
echo "TWS service is not started"
echo "Starting service ..."
$PWD/loader
echo "TWS service was exited!"
Fi
Sleep 10
Done

If you start this shell and discover that the process already exists and that the process was started in a different way instead of the shell, it will continue to remind you to find the process, either by starting the service with this shell, or by killing the service that was started in a different way. That's what the above statement does:
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.