Christopher
Stoll

Old-School UNIX Script: Cluster Command

As a UNIX systems administrator I had to maintain many individual workstations located throughout our facility, and I frequently had to either perform updates on all of the machines or some some subset of them. Logging in to each machine was tedious, even if I was just running a batch script, so I created a script that could send my commands out to all the computers that I listed in a configuration file. The configuration file was a simple text file with a workstation name on each line.

Each of the workstations mounted a common user home directory, so I create a shared home directory for root where all of the administration programs would be located (note that this was not root's actual home directory as that would be problematic if a workstation ever experienced problems accessing the NSF share). With the administrative batch files accessible to all the workstations I could use the script below to initiate the command on the cluster.

#!/bin/sh
######
#
# Run a command on all UNIX workstations, except CAE1
# September 2001, CSTOLL
#
#####

runcommand () {
if [ "$thehost" != '' ]; then
rsh "$thehost" -n "$thecommand"
fi
}

##### Must run from CAE1
#
thishost=`hostname`
if [ "$thishost" != 'cae1.na.xyz.com' ]; then
echo "This script is only to be run on CAE1."
echo "You are attempting to run it on $thishost."
exit 1
fi

##### Check for comand to run
#
if [ -z "$1" ]; then
echo "Syntax: $0 command_to_run"
exit 1
fi

##### Check that the host list exists
#
thehostlist=/xyzuser/root/bin/updaters/hostlist
if [ ! -e "$thehostlist" ]; then
echo "Host list not found! ($thehostlist)"
exit 1
fi

##### Run commands on hosts
#
thecommand=$1
for thehost in `cat "$thehostlist"`
do
echo "$thehost: $thecommand"
runcommand
echo ""
done
Published: 2011-05-24
BloggerUNIXScriptOld-SchoolCode