Re: Master rsync script for many file servers

David <[email protected]> Fri, 12 May 2006 10:05:47 +1000
Newsgroups gmane.org.user-groups.mlug.main
Message-ID <[email protected]>
Geoffrey Scott wrote:
> Geoffrey Scott wrote:
> 
> I decided to call the rsync script from another script as per my text
> below.  But I am having some problems with ssh'ing to another server and
> shutting down services prior to the rsync process.  Can someone help me
> figure out why locally a command will run but not on another server?
> Please see the bottom text.....
> 

> 
> So I define the services I want to shut down:
> SERVICES="smb cups postfix"
> 
> And this works locally:
> for s in $SERVICES; do if [ -e /etc/init.d/$s ]; then /etc/init.d/$s
> start; fi; done
> 
> But not on another server.... :
> for s in $SERVICES; do ssh [email protected] if [ -e /etc/init.d/$s ];
> then /etc/init.d/$s start; fi; done
> 
> Because it says this:
> -bash: syntax error near unexpected token `then'
> 
> Any ideas why this doesn't work?????
> 

Hi Geoff,

Use the return value of an ls(1) .

#!/usr/bin/env bash
HOSTS="hosta hostb hostc"
SERVICES="smb cups postfix"

for host in ${HOSTS} ; do
	for s in ${SERVICES} ; do
		chk=`ssh root@${host} ls /etc/init.d/${s}`
		# get return value of ls
		# will return 1 if not found and zero if found.
		chk_ret=$?
	
		if [ ${chk_ret} -eq 0 ]; then
			ssh root@${host} "/etc/init.d/${s} start"
		else
			echo "Service ${s} on ${host} skipped, not found."
		fi
	
	done
done
# eof

HIH.

David