linux实用的脚本:xcall(同步执行命令)与xsync(同步文件目录)
生活随笔
收集整理的這篇文章主要介紹了
linux实用的脚本:xcall(同步执行命令)与xsync(同步文件目录)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
一.同步文件/目錄腳本(xsync)
- 將文件或目錄同步到集群的其他節(jié)點(diǎn)
- 在使用之前,所有節(jié)點(diǎn)均需要安裝 rsync 服務(wù)(yum install rsync -y)
- 建議創(chuàng)建腳本目錄為:/usr/local/bin
shell腳本
#!/bin/bash# 獲取輸出參數(shù),如果沒有參數(shù)則直接返回 pcount=$# if [ $pcount -eq 0 ] thenecho "no parameter find !";exit; fi# 獲取傳輸文件名 p1=$1 filename=`basename $p1` echo "load file $p1 success !"# 獲取文件的絕對(duì)路徑 pdir=`cd -P $(dirname $p1); pwd` echo "file path is $pdir"# 獲取當(dāng)前用戶(如果想使用root用戶權(quán)限拷貝文件,在命令后加入-root參數(shù)即可) user=$2 case "$user" in "-root")user="root";; "")user=`whoami`;; *)echo "illegal parameter $user"esacecho $user # 拷貝文件到從機(jī)(這里注意主機(jī)的host需要根據(jù)你的實(shí)際情況配置,要與你具體的主機(jī)名對(duì)應(yīng)) for (( host=1;host<=3;host++ )) doecho "================current host is node0$host================="rsync -rvl $pdir/$filename $user@node0$host:$pdir doneecho "complate !"示例
二.同步執(zhí)行命令腳本(xcall)
- 在一個(gè)節(jié)點(diǎn)上輸入命令可以同步到其余節(jié)點(diǎn)執(zhí)行
- 建議創(chuàng)建腳本目錄為:/usr/local/bin
shell腳本
#!/bin/bash# 獲取控制臺(tái)指令cmd=$*# 判斷指令是否為空 if [ ! -n "$cmd" ] thenecho "command can not be null !"exit fi# 獲取當(dāng)前登錄用戶 user=`whoami`# 在從機(jī)執(zhí)行指令,這里需要根據(jù)你具體的集群情況配置,host與具體主機(jī)名一致,同上 for (( host=1;host<=3;host++ )) doecho "================current host is node0$host================="echo "--> excute command \"$cmd\""ssh $user@node0$host $cmd doneecho "excute successfully !"示例
? 使用xcall腳本讓所有節(jié)點(diǎn)使用阿里云同步時(shí)間。
參考鏈接:https://alice.blog.csdn.net/article/details/104439628
總結(jié)
以上是生活随笔為你收集整理的linux实用的脚本:xcall(同步执行命令)与xsync(同步文件目录)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Deepin 20关机提示Unatten
- 下一篇: linux脚本:给定目录下所有文件中查找