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