Monitor the running status of a specified process using Shell scripts.

Source: Internet
Author: User

Monitor the running status of a specified process using Shell scripts.

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.

#! /Bin/shecho "'date'" echo "Start $0 ---------" echo "" # monitor sec = 10 every 10 seconds # obtain the memory usage of the specified process named mainAPP, process running status, process name eval $ (ps | grep "mainApp" | grep-v grep | awk {'printf ("memInfo = % s; myStatus = % s; pName = % s ", $3, $4, $5 )'}) echo $ pName $ myStatus $ memInfotestPrg = "" 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 testPrg = "" doneecho "End $0 --- ($ pName, $ myStatus, $ testPrg) ------------------- "if [-z" $ pName "]; then ### after the tested program exits unexpectedly, stop the test program killall MyTester echo "stop TestProgram MyTester" fiecho "'date'" echo "------------- Current Status --------------------" ps | grep-E "mainApp | SubApp" | grep-v grepecho""

In this way, the status of the process is read every 10 seconds, and then displayed. when the process is found to exist and the status is not Z (non-zombie), the monitoring continues, 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.

Note: eval command usage

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.