Shell script implementation monitors the running state of the specified process


In the previous blog, the implementation method of automatic test program was written. Now developers need to know the running state of the tested process (here refers to the main process running on Linux) before abnormal exit, such as memory utilization, CPU utilization, etc.

Shell script is used to realize the running state of the specified process. Directly on the code.

echo "`date`"
echo "Start $0---------"
echo ""
#Monitor every ten seconds
#Get the specified process name mainapp, memory utilization, 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
while [ -n "$pName" -a "$myStatus" != "Z" ]
    echo "----------`date`---------------------"
    echo $pName $myStatus $memInfo
    sleep $sec
    ####You must initialize them again!!!!!
    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
    ##Be sure to initialize to null again
echo "End $0---($pName,$myStatus,$testPrg)-------------------"
if [ -z "$pName" ]; then
        ###Stop the test program after finding the abnormal exit of the tested program
    killall MyTester
    echo "stop TestProgram MyTester"
echo "`date`"
echo "---------------Current Status------------------"
ps | grep -E "mainApp|SubApp" | grep -v grep
echo ""

The sample implementation reads the status of the process every 10s, and then displays it. When it is found that the process exists and the status is not Z (non zombie), continue to monitor. However, when the test program is executed or exits abnormally, the script will also exit the monitoring cycle. After exiting the cycle, print the status of the specified process.

Recommended Today

SQL server cannot generate fruncm thread

SQL Server was unable to generate the fruncm thread. The database error log is as follows: Copy codeThe code is as follows: 2013-09-26 21:21:50.31 Server      Microsoft SQL Server 2005 – 9.00.1399.06 (Intel X86)Oct 14 2005 00:33:37 Copyright (c) 1988-2005 Microsoft CorporationEnterprise Edition on Windows NT 5.2 (Build 3790: Service Pack 2) 2013-09-26 21:21:50.31 Server      (c) […]