记录一些工作中经常用到的脚本
xcall脚本
用于向集群中的所有主机执行相同的命令
#! /bin/bash
for i in service01 service02 service03
do
echo --------- $i ----------
ssh $i "$*"
done
xsync脚本
同步文件或目录到集群中其他主机上
xsync是对rsync的二次封装,需要先下载好rsync
yum install -y rsync
xsync的脚本为:
#!/bin/bash
#1 获取输入参数个数,如果没有参数,直接退出
pcount=$#
if [ $pcount -lt 1 ]
then
echo Not Enough Arguement!
exit;
fi
#2. 遍历集群所有机器
# 也可以采用:
# for host in hadoop{102..104};
for host in service01 service02 service03
do
echo ==================== $host ====================
#3. 遍历所有目录,挨个发送
for file in $@
do
#4 判断文件是否存在
if [ -e $file ]
then
#5. 获取父目录
pdir=$(cd -P $(dirname $file); pwd)
echo pdir=$pdir
#6. 获取当前文件的名称
fname=$(basename $file)
echo fname=$fname
#7. 通过ssh执行命令:在$host主机上递归创建文件夹(如果存在该文件夹)
ssh $host "mkdir -p $pdir"
#8. 远程同步文件至$host主机的$USER用户的$pdir文件夹下
rsync -av $pdir/$fname $USER@$host:$pdir
else
echo $file does not exists!
fi
done
done
启动zookeeper集群的脚本
#!/bin/bash
case $1 in
"start"){
for i in service01 service02 service03
do
echo ------------- zookeeper $i 启动 ------------
ssh $i "/opt/module/zookeeper/bin/zkServer.sh start"
done
}
;;
"stop"){
for i in service01 service02 service03
do
echo ------------- zookeeper $i 停止 ------------
ssh $i "/opt/module/zookeeper/bin/zkServer.sh stop"
done
}
;;
"status"){
for i in service01 service02 service03
do
echo ------------- zookeeper $i 状态 ------------
ssh $i "/opt/module/zookeeper/bin/zkServer.sh status"
done
}
;;
esac
若您想及时得到回复提醒,建议跳转 GitHub Issues 评论。
若没有本文 Issue,您可以使用 Comment 模版新建。
GitHub Issues