linux 常用脚本
记住写完脚本后都要执行 chmod +x 脚本名称 否则没有执行权限
1.查看集群中的进程 myjps.sh
#!/bin/bash
for host in hadoop102 hadoop103 hadoop104
do
echo "===============$host==================="
ssh $host /opt/module/jdk1.8.0_212/bin/jps
done
2.dfs,yarn,historyserver的群起群停 mycluster.sh 参数 start stop
#!/bin/bash
#校验参数
if [ $# -lt 1 ]
then
echo "Input Args Is Error!!!"
exit
fi
case $1 in
start)
#启动HDFS
echo "=================START HDFS=================="
ssh hadoop102 /opt/module/hadoop-3.1.3/sbin/start-dfs.sh
#启动YARN
echo "=================START YARN=================="
ssh hadoop103 /opt/module/hadoop-3.1.3/sbin/start-yarn.sh
#启动HISTORYSERVER
echo "=================START HISTORYSERVER=================="
ssh hadoop102 /opt/module/hadoop-3.1.3/bin/mapred --daemon start historyserver
;;
stop)
#停止HDFS
echo "=================STOP HDFS=================="
ssh hadoop102 /opt/module/hadoop-3.1.3/sbin/stop-dfs.sh
#停止YARN
echo "=================STOP YARN=================="
ssh hadoop103 /opt/module/hadoop-3.1.3/sbin/stop-yarn.sh
#停止HISTORYSERVER
echo "=================STOP HISTORYSERVER=================="
ssh hadoop102 /opt/module/hadoop-3.1.3/bin/mapred --daemon stop historyserver
;;
*)
echo "Input Args Is Error!!!"
;;
esac
3.zookeeper的群起群停 zk.sh 参数 start stop status
#!/bin/bash
if [ $# -lt 1 ]
then
echo "Input Args Is Error!!!"
exit
fi
#循环遍历每一台机器,操作zk
for host in hadoop102 hadoop103 hadoop104
do
case $1 in
"start")
echo "=================start $host zookeeper==================="
ssh $host /opt/module/zookeeper-3.5.7/bin/zkServer.sh $1
;;
"stop")
echo "=================stop $host zookeeper==================="
ssh $host /opt/module/zookeeper-3.5.7/bin/zkServer.sh $1
;;
"status")
echo "=================status $host zookeeper==================="
ssh $host /opt/module/zookeeper-3.5.7/bin/zkServer.sh $1
;;
*)
echo "Input Args Is Error!!!"
exit
;;
esac
done
4.kafka的群起群停 kafka.sh 参数 start stop
#!/bin/bash
case $1 in
start)
for host in hadoop102 hadoop103 hadoop104
do
echo "===============start $host kafka==================="
ssh $host /opt/module/kafka/bin/kafka-server-start.sh -daemon /opt/module/kafka/config/server.properties
done
;;
stop)
for host in hadoop102 hadoop103 hadoop104
do
echo "===============stop $host kafka==================="
ssh $host /opt/module/kafka/bin/kafka-server-stop.sh
done
;;
esac
5.xcall 里面封装了pdsh命令 可以在指定的一组机器上执行同一个命令 其他脚本可以在这基础上封装 xsync
#!/bin/bash
[ -z "$CLUSTER" ] && CLUSTER="hadoop102 hadoop103 hadoop104"
pdsh -w "$CLUSTER" "$*" | sort -k1 | awk -F ": " '{if (host!=$1) {host=$1;print ">>>>>>>>>>>> "host" <<<<<<<<<<<<"};$1=null;print $0 }'
6.同步文件 xsync
#!/bin/bash
#1. 判断参数个数
if [ $# -lt 1 ]
then
echo Not Enough Arguement!
exit;
fi
#2. 遍历所有文件
for file in $@
do
#4. 判断文件是否存在
if [ -e $file ]
then
#5. 获取父目录
pdir=$(cd -P $(dirname $file); pwd)
#6. 获取当前文件的名称
fname=$(basename $file)
xcall "mkdir -p $pdir"
xcall "rsync -av $(hostname):$pdir/$fname $pdir"
else
echo $file does not exists!
fi
done