top: filter by command, user, CPU usage and time running

Posted on 2011-10-17
Last Modified: 2012-05-12
I want top to show ONLY processes that match the following:
Command: PHP
User: foobar
CPU Usage: Greater than 10%
Time: Has run for at least 30 seconds

Question by:hankknight
    1 Comment
    LVL 37

    Accepted Solution

    Don't think you can do this with top as it cannot filter on the PHP command. It can filter a user but not cpu > 10% and time > 30s.

    I would recommend a script that starts with a top command like this:
    top -b -n1 -u foobar | grep foobar | grep PHP

    Open in new window

    This will give you output with on the first line (if they exists) a list of PHP process of user foobar with the current CPU load in column #9

    Then filter the output for CPU load > 10% with awk, like this:
    top -b -n1 -u foobar | grep foobar | grep PHP | awk '$9 > 10.0'

    Open in new window

    Last filter again on column 11 that has cumulative CPU time, like this:
    top -b -n1 -u foobar | grep foobar | grep PHP | awk '$9 > 10.0' | awk '$11 > "0:30.00"'

    Open in new window

    Put in a small script that adds a loop and a sleep, like this for example:
    while [ 1 = 1 ]
    top -b -n1 -u foobar > tl.txt
    head -7 tl.txt > hdr.txt
    cat tl.txt | grep foobar | grep PHP | awk '$9 > 10.0' | awk '$11 > "0:30.00"' > pl.txt
    cat hdr.txt pl.txt
    sleep 3

    Open in new window

    The script above will start displaying your 'top' with 3s intervals. Note that you will only see a header when the filter criteria are not met. You may have to change column numbers depending your top output.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Suggested Solutions

    Daily system administration tasks often require administrators to connect remote systems. But allowing these remote systems to accept passwords makes these systems vulnerable to the risk of brute-force password guessing attacks. Furthermore there ar…
    Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
    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…
    Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

    760 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

    9 Experts available now in Live!

    Get 1:1 Help Now