如何在shell / bash脚本中运行下一个命令之前等待第一个rsync进程完成

问题描述 投票:0回答:1

下面是我的脚本。基本上,我只想通过调出此脚本从其他服务器复制文件。一些文件很大,发生的情况是它将在完成之前杀死第一个rsync命令,然后继续执行下一个。我尝试使用屏幕命令,但不确定如何在shell / bash中编码Ctrl + a d(以分离)。

HFDIR=/var/opt/ubkp/data/local/prework/hotfixes
RODIR=/var/opt/ubkp/data/local/prework/rollouts
THFDIR=$(ls -t /var/opt/ubkp/data/local | grep hotfix | head -1)
TRODIR=$(ls -t /var/opt/ubkp/data/local | grep rollout | grep -v check | head -1)
user=$(/usr/seos/bin/sewhoami)
if [ $user = "root" ]; then
echo "This script should not be run as the TRUE root user"
echo "Log in so that \"sewhoami\" does not display \"root\" and then execute this script."
exit
else
#list of ROs and HFs 
list=/tmp/list.txt
echo -n "Enter Password: "
read -s PWD
# first rsync command
/usr/bin/expect<<EOD
spawn rsync -a $user@server:$HFDIR/* /var/opt/ubkp/data/local/$THFDIR
expect "assword"
send "$PWD\r" 
wait $!
expect eof
EOD
# second rsync command
/usr/bin/expect<<EOD
spawn rsync -a $user@server:$RODIR/* /var/opt/ubkp/data/local/$TRODIR
expect "assword"
send "$PWD\r"
expect eof
EOD
fi
exit
linux bash shell screen rsync
1个回答
0
投票

您的第二个rsync将在10秒后被杀死,因为这是expect eof的默认超时时间。您应该在发送后添加wait,以便永远等待直到过程结束。

此外,您还应该删除$!中的wait。它是一个shell变量,而不是期望变量。幸运的是,在这种情况下$!为空,因为您没有在后台使用&在外壳程序中运行任何命令。

© www.soinside.com 2019 - 2024. All rights reserved.