Monitor CPU and RAM with a shell script

Posted on 2006-04-09
Last Modified: 2008-03-03
Hello all,

I want to write a small shell script for Linux red-hat 3.0 that will automatically monitor the CPU usage and memory.

My plan is to put the script in a cron and write to a file the results every X seconds.
In the end I want to perform an average on all results I have.

I know the command 'top' but not quite sure how to extract the data from its output.
What I'm looking for if possible is something like:
CPU X% free
RAM X% free

The script should run while I'm keeping the machine busy so the I'll use the results for performance analsys.

Question by:jinkaz
    LVL 34

    Accepted Solution

    You should not expect there to be any RAM free in a busy system, no matter how much you have. Linux will use it all for i/o/buffers and cache. You can choose to regard those as "free" although small values may correlate with reduced performance. For %idle, run top in batch mode, 1 iteration, take only the first 3 lines:

    $ top -b -n 1|head -3
    top - 07:29:09 up 7 days, 17:47, 10 users,  load average: 0.02, 0.03, 0.00
    Tasks: 106 total,   1 running, 105 sleeping,   0 stopped,   0 zombie
    Cpu(s):  0.1% us,  0.1% sy,  0.0% ni, 99.8% id,  0.0% wa,  0.0% hi,  0.0% si

    You just want the idle, so extend the command pipe:

    $ top -n 1|head -n 3|tail -n 1|awk '{print $8}'
    LVL 17

    Assisted Solution

    You can try with Big Brother.

    Best solution ever I  saw is Big Brother. You can install Big brother professional server in a one machine.
     And you can installl Big brohter client on other machines.
    Then you can view in web base CPU, HardDisk, EMAIL, FTP, SSH, HTTP, ..etc is up or not.
    Please download windows installation from
    If you need any help regarding BB Installation plz reply to me.
    BR Dushan
    LVL 7

    Assisted Solution

    rather than beating top into shap, grab your raw data from
    /proc/freemem and /proc/loadavg

    Now the standard for doing this is mrtg + snmp + sysstat

    This gives you pretty usage graphs udated every 5 minutes.
    you can also run vmstat

    vmstat 1

    will print off all the important stats every second (note that your processor has to run vmstat so some of the load
    is the monitor itself.)
    procs -----------memory---------- ---swap-- -----io---- --system-- ----cpu----
     r  b   swpd   free   buff  cache   si   so    bi    bo        in    cs      us sy id wa
     0  0   7312   7276  19456 127108    0      0     0         0    110    12  0  1 99  0
     0  0   7312   7276  19456 127108    0      0     0         0    108    11  1  0 99  0
     0  0   7312   7276  19456 127108    0      0     0         0    106    14  0  1 99  0

    (Yes this is a low end system)
    The key stats are swap (so = page swapped to disk, si page swapped from disk)
    io (bi = blocks read, bo = blocks written)
    and cs (context switches...literally how many running processes)

    vmstat gives frightening stats unless you run it for longer than one count.

    Oh, and its installed by default on most linux platforms

    Author Comment

    Is vmstat better than top ? by saying better I mena performance wise (I presume that both vmstat and top are accurate in reading the system stats).

    I'll try putting vmstat in a cron so it will write the stats to a file every 10 seconds. hopefully it will no affect the performance too much.
    LVL 51

    Assisted Solution

    I'd prefer vmstat 'cause it uses the core /proc files
    LVL 7

    Expert Comment

    If you want better information, install mrtg and sysstat, you might have to update a couple of perl scripts, but then you can get
    continuously updating CPU and RAM graphs so you can monitor trends.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    Suggested Solutions

    Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
    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…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    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.

    729 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

    19 Experts available now in Live!

    Get 1:1 Help Now