Monitoring with top command

Posted on 2012-09-21
Last Modified: 2012-09-25
Hi Experts,

I am monitoring a linux system with top command for a particular process. If that process takes more than 50% of CPU , it should email me. How can be done this with scripting.
Question by:07592161981m
    LVL 11

    Expert Comment

    Q1. How long should a process suck more than 50% CPU for you to be emailed, 07592161981m?
    Q2. More than 50% of one CPU, or the total CPU of the system (or is it just a single CPU system)?
    LVL 37

    Expert Comment

    by:Gerwin Jansen
    Use ps command in batch mode after which you filter for your process name. Then grep the column that has the CPU usage % and with an if/then construction send mail (or not).

    Author Comment

    Hi Tel2,

    The CPU should be sucked 50% atleast 60 seconds. It is a single CPU system.
    LVL 37

    Accepted Solution

    Here's a sample script that will monitor the process firefox for a max. 50% cpu usage. It uses top -b to get current cpu usage, if above limit, a counter is increased. When max. is reached you can have it send a mail. Script takes a sample every 10s - if you need more resolution, change sleep to 5s and the if count to 12 instead of 6.
    while [ 1=1 ]
            usage=$(top -b -n 1 | grep ${process} | awk '{ print $10 }')
            if [ $(echo "${usage} > ${limit}"|bc) -eq 1 ]
                    # usage above target setting
                    let count=${count}+1
                    echo "Usage: of ${process} ${usage} > ${limit} - increasing counter - count is now ${count}"
                    if [ ${count} -gt 6 ]
                            echo "Usage of ${process} > ${limit} for too long - sending mail"
                            # put your mail command here
                    # usage below target setting
                    echo "Usage: of ${process} ${usage}"
            sleep 10

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Suggested Solutions

    The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
    Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
    Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now