今年在公司主要工作是交接三千多台的Elasticsearch机器, 经常需要批量部署和修改配置文件,所以这里整理下所需要的批量执行命令和脚本。
单参数批量执行
单纯执行脚本

   for i in `cat list` ;do echo $i;sudo ssh $i "common1";sudo ssh $i "common2";done

判断之后执行

   #!/bin/bash 
   #
   #
   for i in `grep -v "#" ip.txt`
   do
   sudo ssh $i "ls" > /dev/null 2>&1
   if [ $? -eq 0 ];then
            common1
        echo "$i" >> sucess.txt
   else
        echo "$i" >> error.txt
   fi
   done

多参数批量执行

 #!/bin/bash
 while read line
 do
 ip=$(echo $line | awk '{print $1}')
 name=$(echo $line | awk '{print $2}')
 echo ${ip} 
 echo ${name}
 sudo ssh ${ip} "mkdir /opt/es " < /dev/null
 echo ----${ip} download file sucess-----
 sudo ssh ${ip} "sh /opt/es/xxx.sh" < /dev/null
 echo ----${ip} es install sucess-----
 done <list

上面的代码主要困难 主要要加 < /dev/null,如果没有这个参数,第一次循环就会退出!
下面写批量杀进程技巧

for i in `cat list`;do echo $i;sudo ssh $i "ps -ef |grep elasticsearch|grep -v grep |awk '{print \$2}'|xargs kill -9";dnoe

之前一直不成功 主要是 $2 之前没有\ 这个转义字符,这也算是一个坑吧! 毕竟坑还是很多的。
find 查找日志后删除

find /opt/eshome/elasticsearch-1.7.0/logs/ -mtime +0  -name "*.log.*" |xargs rm -f 

find查找日志后 置空 不能用xargs >

for i in `find . -name "*.log"`;do  cat /dev/null >$i;done 

未完待续