在上个章节中我们要在5个节点上均要创建/home/hadoop/app、/home/hadoop/tools、/home/data目录,我们是在每个节点上执行mkdir命令,这样是不是很啰嗦,效率很低,于是采用shell脚本实现多节点批处理是明智之举。
在这里,我写1个配置文件与2个shell脚本(deploy.conf、deploy.sh、runRomteCmd.sh);
一、deploy.conf的代码如下:
#### NOTES
# There is crontab job using this config file which would compact log files and remove old log file.
# please be carefully while modifying this file until you know what crontab exactly do
#hdp
hadoop001,all,namenode,zookeeper,resourcemanager,
hadoop002,all,slave,namenode,zookeeper,resourcemanager,
hadoop003,all,slave,datanode,zookeeper,
hadoop004,all,slave,datanode,zookeeper,
hadoop005,all,slave,datanode,zookeeper
二、deploy.sh的代码如下:
#!/bin/bash
#set -x
if [ $# -lt 3 ]
then
echo "Usage: ./deply.sh srcFile(or Dir) descFile(or Dir) MachineTag"
echo "Usage: ./deply.sh srcFile(or Dir) descFile(or Dir) MachineTag confFile"
exit
fi
src=$1
dest=$2
tag=$3
if [ 'a'$4'a' == 'aa' ]
then
confFile=/usr/hadoop/tools/deploy.conf
else
confFile=$4
fi
if [ -f $confFile ]
then
if [ -f $src ]
then
for server in `cat $confFile|grep -v '^#'|grep ','$tag','|awk -F',' '{print $1}'`
do
scp $src $server":"${dest}
done
elif [ -d $src ]
then
for server in `cat $confFile|grep -v '^#'|grep ','$tag','|awk -F',' '{print $1}'`
do
scp -r $src $server":"${dest}
done
else
echo "Error: No source file exist"
fi
else
echo "Error: Please assign config file or run deploy.sh command with deploy.conf in same directory"
fi
三、runRomteCmd.sh的代码如下:
#!/bin/bash
#set -x
if [ $# -lt 2 ]
then
echo "Usage: ./runRemoteCmd.sh Command MachineTag"
echo "Usage: ./runRemoteCmd.sh Command MachineTag confFile"
exit
fi
cmd=$1
tag=$2
if [ 'a'$3'a'=='aa' ]
then
confFile=/usr/hadoop/tools/deploy.conf
else
confFile=$3
fi
if [ -f $confFile ]
then
for server in `cat $confFile|grep -v '^#'|grep ','$tag','|awk -F',' '{print $1}'`
do
echo "*******************$server***************************"
ssh $server "source /etc/profile; $cmd"
done
else
echo "Error: Please assign config file or run deploy.sh command with deploy.conf in same directory"
fi