views:

53

answers:

2

i have access to a few linux clusters at school. sometimes they are busy and sometimes they are not. i usually log into each machine manually, use the "top" command to see if the server is busy (to see if both cores are used). from there, i execute my program to use some servers that are free to run my computations.

what i'd like to do is to automate this process with a script. suppose i have a list of servers, say server1 ... server N. I'd like to log into each of these servers sequentially, run some command (top?), and output a list of servers that are unused (or output the top two processes, showing cpu %, for each server).

any suggestions would be greatly appreciated.

A: 

Your question made me curious. After voting to close it, I found the w command.

If you were to do something like

echo Host: host
ssh vinh@host w

in a script, with one pair of lines for each host you'd like to visit, you could get roughly what you wanted. You could finesse it a bit with head -1 on the output from w to see only the load line.


Update, improved:

for h in host1 host2 host3; do
echo host: $h `ssh vinh@$h w | head -1`
done

Update, improved some more:

for h in host1 host2 host3; do
echo host: $h `ssh vinh@$h uptime`
done
Carl Smotricz
...or you could use `uptime` instead of `w | head -1` ;)
caf
I never thought to look for that info in uptime... thanks!
Carl Smotricz
A: 

Thanks for the suggestions. Here is my script for anyone that is interested:

#! /usr/bin/env bash

out=avail.txt
rm -rf ~/$out
minLoad=1
for h in $(cat ~/listofservers.txt); do
    ##w | head -1 | cut -d : -f 5 - | cut -d "," -f 2 -
    load=`ssh username@$h uptime | cut -d : -f 5 - | cut -d "," -f 2 -`
    comparison=`expr $load \< $minLoad`
    if [ comparison ]; then
        echo "$h" >> ~/$out
        ##echo "$load" >> ~/$out
    fi
done
Vinh Nguyen