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.