我有一个脚本,它依次触发另一台服务器中的另外 4 个脚本。我的脚本正在等待,直到第一个脚本在目标服务器中完成,然后触发第二个脚本。下面是代码
SCB_CMD=/sc/db2home/scbinst/bin/reload_scrpt1.sh
SCB_LOG=/sc/db2home/scbinst/log/reload_scrpt1.log
echo `date` "Executing $SCB_HOST:$SCB_CMD ..."
ssh $SCB_HOST "$SCB_CMD | tee $SCB_LOG"
RC=$?
#---------------------------------------------------------------------------
# -- Check for errors
#---------------------------------------------------------------------------
if [ $RC -ne 0 ]
then
echo `date` "!error occurred executing SCB load script1!"
exit 99
fi
#---------------------------------------------------------------------------
SCB_CMD=/sc/db2home/scbinst/bin/reload_scrpt2.sh
SCB_LOG=/sc/db2home/scbinst/log/reload_scrpt2.log
#---------------------------------------------------------------------------
# -- Execute the remote load script
#---------------------------------------------------------------------------
echo `date` "Executing $SCB_HOST:$SCB_CMD ..."
ssh $SCB_HOST "$SCB_CMD | tee $SCB_LOG"
--------------------------------------------
有没有办法在目标服务器中并行触发所有这四个脚本?
答案1
&
命令末尾将作业置于后台。
我会重写我的剧本
连接器.sh
#!/usr/bin/env bash
echo `date` "Executing $1:$2"
# 1 host, 2 cmd, 3 log
ssh $1 "$2 | tee $3"
添加 runner.sh 脚本
#!/usr/bin/env bash
connector.sh 127.0.0.1 /sc/db2home/scbinst/bin/reload_scrpt1.sh /var/log.log &
connector.sh 127.0.0.1 /sc/db2home/scbinst/bin/reload_scrpt2.sh /var/log.log &
connector.sh 127.0.0.1 /sc/db2home/scbinst/bin/reload_scrpt3.sh /var/log.log &
答案2
您可以并行启动作业(我假设这些ssh
命令在命令行上工作,您单独运行它们,这意味着,远程相对于本地 m/c 问题不存在任何问题。
for i in `seq 1 4`; do
SCB_CMD=/sc/db2home/scbinst/bin/reload_scrpt${i}.sh
SCB_LOG=/sc/db2home/scbinst/log/reload_scrpt${i}.log
echo `date` "Launching $SCB_HOST:$SCB_CMD ..."
ssh "$SCB_HOST" "$SCB_CMD | tee $SCB_LOG" &
sleep 1
done