A shell script that automatically restarts after a Linux monitoring process crashes and hangs

Source: Internet
Author: User

How do I keep the service running? How can I ensure that the service is automatically restarted even if it is hung up? This is often a problem when you write a service program. In a Linux system, a powerful shell can handle such transactions with great flexibility.

The following shell through a while-do loop, with Ps-ef|grep to check whether the loader process is running, if not run, then start, so that the crash hangs the process of re-started in time.

Two points must be noted:

1, PS |grep a process must add its road strength, or easily grep to the wrong result;

2, you must use-V to remove the grep command itself from the results, otherwise the result is not empty.

Copy the Code code as follows:

#!/bin/sh

While:

Do

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"

echo "Kill it and then the startup by this shell, and the other wise this shell would 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 is started in another way instead of the shell, it will continue to alert you to the process, either by starting the service only with this shell, or by killing a service that is started otherwise. This is how the above statement is done:

Kill-9 $pidof $PWD/loader

A shell script that automatically restarts after a Linux monitoring process crashes and hangs

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.