Hi, i wonder if someone can help me with this,

I am trying to get the results of top across multiple machines at the same time. I only care about the first process listed by 'top'. I can't figure out how to make this happen. I have got passwordless SSH, and have made the following script to extract the entry i care about from top.

top | sed -n '
#Dont print all lines
/PID/ {
#if PID is found
#get the next line
#print it

I dont know how to proceed to make it a) work across multiple machines simultaneously, if i ssh into another machine i get an error such as 'TERM environment variable not set', and if i run it in the background with & it just stops. b) make the output go onto the same line each time, instead of filling up my console with line after line of the same kind of output. The type of output i would like would be similar to top, just refreshing the output over the same lines.

Any kind of input towards helping me with either problem would be greatly appreciated!


I suggest you start top in batch mode and with the number of iterations set to 1. This way you take a a quick snapshot of things and save it to a file.

There is the -f option with top which will help you re-direct the output to a file and then you can apply sed on the output file.

Thanks for the tips guys, they pointed me in the right direction. If anybody else wants to do a similar thing, here's the script i came up with in the end:

#IP addresses of Hosts to check must be in that file

while [ "$response" != q ]
  tput cup 1 0
  echo "                                                                                "
  tput cup 1 0
  top -b -n 1 | sed -n ';/PID/ {;n;s/[^a-z]*\([a-z]*\)[^0-9a-z]*[0-9]*[^0-9a-z]*[0-9]*/'$thisIP'\t\1/p;}' |sed -e 's/[[:space:]][[:space:]]*/\t/g' -e 's/\([^[:space:]]*\t[^[:space:]]*\t[^[:space:]]*\t[^[:space:]]*\t\)[^[:space:]]*\t/\1/' -e 's/\([0-9]*:[0-9]*\)\.[0-9]*/\1/g'
  cat $FILE | while read line
    cnt=$(($cnt + 1))
    thisIP=$(echo $line | sed 's/[0-9]*\.[0-9]*\.[0-9]*\.//')
    tput cup $cnt 0
    echo "                                                                                "
    #Assumes an 80 character line
    tput cup $cnt 0
	  ssh -n -XC -p 22111 $line " top -b -n 1 "| sed -n ';/PID/ {;n;s/[^a-z]*\([a-z]*\)[^0-9a-z]*[0-9]*[^0-9a-z]*[0-9]*/'$thisIP'\t\1/p;}' |sed -e 's/[[:space:]][[:space:]]*/\t/g' -e 's/\([^[:space:]]*\t[^[:space:]]*\t[^[:space:]]*\t[^[:space:]]*\t\)[^[:space:]]*\t/\1/' -e 's/\([0-9]*:[0-9]*\)\.[0-9]*/\1/g'
  echo "--------------------------------------------------------------------------------"
  echo "Type q to quit or anything else to refresh sooner"
  read -s -n1 -t 5 response
  #Will accept keypress, but wont do anything about it till it reaches here

The script also formats the output so it has the columns i wanted, as well as displaying the output from the current machine. It assumes that the file $HOME/mpd.hosts contains a list of IP addresses, 1 per line, that you can ssh to without a password.