R-Ts NetWorks

Server the Best

Top command in batch mode


Run the following command to begin top in batch mode, logging to a file called TOP_LOG:

top icbd .1 | egrep -v “top|Tasks|Cpu|Mem|Swap|PID|top icbd|^$|tee -a” | tee -a top_log

  1. Code breakdown:
    top icbd .1 Run the top command with the idle processes being shown, full command line paths being shown, running inbatch mode, and with the delay set to .1seconds so that it quickly refreshes.
    egrep -v “top|Tasks|Cpu|Mem|Swap|PID|top icbd|^$|tee -a” Use the egrepcommand with the -vflag to not show any lines that include top,TasksCpuMem,SwapPIDtop icbd,^$ which is any blank line, or tee -a. This way we only see lines from top that have process information.
    tee -a TOP_LOG Finally use the teecommand with the -append flag to simultaneously write the data from the topcommand out to a file called TOP_LOG.
  2. After you’ve let this run for some time to gather data, you can go ahead and hit Ctrl-C to stop thetop command from gathering more data.
  3. cat TOP_LOG| sort -k9| tail -20| sort -k2
  4. You should end up with something that looks like this:
  5. —————————-
    14238 askhacke 17 0 201m 42m 21m R 97.4 0.4 0:00.20 /usr/bin/php /home/askhacke/public_html/index.php
    14238 askhacke 17 0 217m 72m 34m R 97.3 0.6 0:00.42 /usr/bin/php /home/askhacke/public_html/index.php
    14186 askhacke 18 0 217m 72m 34m R 99.1 0.6 0:00.71 /usr/bin/php /home/askhacke/public_html/index.php
    14186 askhacke 18 0 217m 72m 34m R 99.2 0.6 0:00.92 /usr/bin/php /home/askhacke/public_html/index.php
    14131 gotomyin 17 0 193m 27m 14m R 99.1 0.2 0:00.11 /usr/bin/php /home/gotomyin/public_html/showthread.php
    20510 gotomyin 17 0 197m 34m 16m R 97.4 0.3 0:00.17 /usr/bin/php /home/gotomyin/public_html/newthread.php
    14064 integrat 17 0 196m 35m 18m R 98.1 0.3 0:00.14 /usr/bin/php /home/integrat/public_html/wp-admin/admin-ajax.php
    14262 jollence 17 0 205m 51m 25m R 99.0 0.4 0:00.25 /usr/bin/php /home/jollence/public_html/wp-cron.php
    —————————-
  6. Once you are done reviewing the data in our TOP_LOGfile you can run the following command to remove this file:rm -rf ./TOP_LOG
  7. Thanks to inmotionhosting.com  Jacob Nicholson 🙂
Advertisements

March 19, 2013 Posted by | Linux | Leave a comment