Monitoring of shell scripts specifies the running state of a process

Source: Internet
Author: User
Tags cpu usage shebang

In the previous blog, the implementation of the automated test program was written, and now the developer needs to know the running state of the process that was tested (in this case, the main process running on Linux) before the exception exited, such as memory usage, CPU usage, and so on.
The running state of the specified process is now implemented with a shell script.
directly on the code.

#!/bin/shEcho "' Date '"Echo "Start ---------"Echo ""#每十秒监视一下Sec=Ten#取得指定进程名为mainAPP, memory usage, process run state, process nameEval$ (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 $memInfoSleep$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 $} '`if[-Z"$testPrg"]; Then                 Break        fi        # #注意一定要再次初始化为空testprg="" DoneEcho "End $---($pName,$myStatus,$testPrg)-------------------"if[-Z"$pName"]; Then                # # #发现测被测试程序异常退出后, stop the test programKillall MytesterEcho "Stop Testprogram mytester"fiEcho "' Date '"Echo "---------------current Status------------------"PS | Grep-e"mainapp| Subapp "| Grep-v grepEcho ""

This realizes that every 10s reads the status of the process, and then displays, when the discovery process exists and the state is non-Z (non-zombie), continue to monitor, but when the test program completed or an abnormal exit, the script will also exit the monitoring loop, after exiting the loop, print out the status of the specified process.

Note: Use of the eval directive

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Monitoring of shell scripts specifies the running state of a process

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.