Skip to content
Home » Unix » How to Monitor UNIX CPU Usage by Process

How to Monitor UNIX CPU Usage by Process

OP usually have been told and trained to use "top" command to monitor the overall status of a server, the "top" command can show many useful information like PID, USERNAME, CPU, RES, COMMAND, etc. It sorts CPU utilization percentage by defaults, but the column COMMAND is too simple to judge which processes are taking most of CPU resource, you have to see the details by "ps" command such as the following to find the correct process name with a known $PID.
[oracle@primary01 ~]$ ps -ef | awk '{$2 ~ /^$PID$/}'

The above method does work on Solaris platform, but it's very inconvenient to operate by human been. If a DBA give an order to OP to record the top ten cpu utilization processes of a database server on a specific window every 10 by this indirect method, they might complaint all the day. So it would be better to write a script for them to use.

Of course, OP can be trained to use more complex and various combination command to identify the most cpu-consuming processes directly by "ps" command like this:
[oracle@primary01 ~]$ ps -eo pid,user,pcpu,args | sort -k3rn

But a formatted result would be better and welcomed. The script in the following demonstration will try to collect enough information from top, sar and ps to compose a readable format and can be rendered to a report for reviewing by management.
  1. Compose a script to retrieve resource information.
  2. $ vi cpu_usage.sh

    #!/bin/bash
    clear
    echo "          DateTime: "`date '+%Y-%m-%d %T'`
    echo ""
    top -n 0 | egrep 'load averages|Memory:'
    echo "CPU idle: "`sar -i 300 | tail -3 | head -1 | awk '{print $5}'`"%"
    echo ""
    echo " PID    USER    CPU  MEM   START   PROCESS_NAME"
    echo "-----------------------------------------------------------"
    ps -eo pid,user,pcpu,pmem,stime,args | sort -k3rn | head
    echo "==================================================================="

  3. Compose a script for easily logging and can be added to the crontab.
  4. $ vi run_cpu_usage.sh

    #!/bin/bash
    WORKING_DIR=/home/oracle/scripts
    $WORKING_DIR/cpu_usage.sh >> $WORKING_DIR/cpu_usage_`date +%Y%m%d`.log

  5. Change the permissions for the script files.
  6. $ chmod u+x *.sh
  7. Test the script and see the result.
  8. $ ./run_cpu_usage.sh

              DateTime: 2012-11-16 19:24:35
    load averages:  4.02,  3.07,  2.79    19:24:35
    Memory: 16.0G real, 3.8G free, 9.6G swap in use, 9.8G swap free
    CPU idle: 48%
     PID    USER    CPU  MEM   START   PROCESS_NAME
    -----------------------------------------------------------
    12363   oracle  8.4  0.5 18:37:08 oraclePRIMDB1 (LOCAL=NO)
    12435   oracle  7.6  0.5 18:47:08 oraclePRIMDB1 (LOCAL=YES)
     8483   oracle  6.4  0.5   Nov_14 ora_lgwr_PRIMDB1
     8449   oracle  4.1  0.2   Nov_14 ora_lmd0_PRIMDB1
     8477   oracle  3.3  0.5   Nov_14 ora_arc0_PRIMDB1
     8465   oracle  3.1  0.2   Nov_14 ora_smon_PRIMDB1
     8481   oracle  2.6  0.5   Nov_14 ora_dbw0_PRIMDB1
     8453   oracle  2.0  0.2   Nov_14 ora_lms1_PRIMDB1
     8451   oracle  1.9  0.2   Nov_14 ora_lms0_PRIMDB1
    13590   oracle  1.8  0.2 19:23:33 extprocPLSExtProc (LOCAL=NO)
    ===================================================================

  9. Add an entry to the crontab.
  10. $ crontab -e
    ...
    */10 9-17 * * 1-5 /home/oracle/scripts/run_cpu_usage.sh

    If you would like to run this job through root, you can add an entry like this:
    # crontab -e
    ...
    */10 9-17 * * 1-5 su - oracle -c '/home/oracle/scripts/run_cpu_usage.sh'

    This entry means that the job must start every 10 minutes, from 9 a.m. to 5 p.m. on weekdays.
  11. OP can tail the result by following the log file.
  12. $ tail -f cpu_usage_`date +%Y%m%d`.log
    Since we add a "clear" in the begging of the retrieving script, OP could monitor the newest result during tailing in some terminal.
Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *