The Shell script monitors the running status of a specified process.

Source: Internet
Author: User

The Shell script monitors the running status of a specified process.

This article mainly introduces how to monitor the running status of a specified process using Shell scripts. This article provides the script code directly. For more information, see

In the previous blog, I wrote the implementation method of the automated testing program. Now developers need to know the process to be tested (here it refers to the main process running on LINUX) the running status of the process before exiting, such as memory usage and CPU usage.

A shell script is used to run the specified process. Directly add the code.

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

#! /Bin/sh

Echo "'date '"

Echo "Start $0 ---------"

Echo ""

# Monitoring every 10 seconds

Sec = 10

# Obtain the specified process name mainAPP, memory usage, process running status, and process name

Eval $ (ps | grep "mainApp" | grep-v grep | awk {'printf ("memInfo = % s; myStatus = % s; pName = % s", $3, $4, $5 )'})

Echo $ pName $ myStatus $ memInfo

TestPrg = ""

While [-n "$ pName"-a "$ myStatus "! = "Z"]

Do

Echo "---------- 'date '---------------------"

Echo $ pName $ myStatus $ memInfo

Sleep $ sec

#### You must initialize them again !!!!!

PName = ""

MyStatus = ""

MemInfo = ""

Eval $ (ps | grep "mainApp" | grep-v grep | awk {'printf ("memInfo = % s; myStatus = % s; pName = % s", $3, $4, $5 )'})

TestPrg = 'ps | grep "MyTester" | grep-v grep | awk '{print $0 }''

If [-z "$ testPrg"]; then

Break

Fi

# Be sure to initialize it again as null

TestPrg = ""

Done

Echo "End $0 --- ($ pName, $ myStatus, $ testPrg )-------------------"

If [-z "$ pName"]; then

### Stop the test program after the tested program exits unexpectedly

Killall MyTester

Echo "stop TestProgram MyTester"

Fi

Echo "'date '"

Echo "--------------- Current Status ------------------"

Ps | grep-E "mainApp | SubApp" | grep-v grep

Echo ""

The Process status is read every 10 seconds, and then displayed. when the process is found to exist and the status is not Z (non-zombie), continue monitoring, however, when the test program is executed or unexpectedly exits, the script also exits the monitoring loop and then prints the status of the specified process.

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.