title: top command in shell script
body: <p>I am trying to get first 5 lines of top command through expect script. Im calling this expect script from a shell script along with some other stuff.</p>
  
  <p><code>top | head -5</code> gives me below output ie without cpu stats-</p>
  
  <blockquote>
    <p>top - 09:10:58 up 46 days, 17:03, 12 users,  load average: 0.01, 0.02,
    0.00 Tasks: 138 total,   1 running, 137 sleeping,   0 stopped,   0 zombie</p>
    
    <p>Mem:  16432400k total,  8408096k used,  8024304k free,   609200k
    buffers Swap:  6290736k total,        0k used,  6290736k free, 
    6754356k cached</p>
  </blockquote>
  
  <p>If I run just <code>top</code> command on that remote server I can see there is a 2-3 second delay before the CPU states line is updated, can some one please help me to get all 5 lines with updated cpu states? Below is my expect script -</p>
  
  <pre><code>#!/usr/bin/expect -f
  set user1 abc
  set pass1 pass
  set timeout 8
  match_max 1000
  spawn ssh -C -o stricthostkeychecking=no $user1@&lt;ip&gt;
  expect "*?assword:*"
  send  "$pass3\r"
  expect "?xterm*"
  send "\r"
  send "top | head -5\r"
  expect eof
  </code></pre>