实现多台主机同时运行一个shell命令的自定义命令:
- 其实就是使用ssh命令加上循环实现的
-
在一台主机上编写脚本together_cmd:
#####ssh到别的主机执行命令。 command=$1 echo "start do command:$command" for host_ip in 10.153.131.{60..65} do echo "#########$host_ip##############" ssh ${host_ip} " . ~/.bash_profile;$command" & done
-
将此自定义命令加入环境变量:
编辑
~/.bash_profile
文件将alias dzcmd="sh /jlbillpkg/shell/dzcmd"
添加进去[test@bossdz19]:/billdata/test>vim ~/.bash_profile
-
执行想多台主机同时执行的命令:
together_cmd "echo hello" # 也可以执行脚本,前提是多台主机同一个位置都存在这个脚本 together_cmd "sh test.sh"