我在shell脚本中有一个代码如下:
# Setup the command.
command=`ec2-describe-snapshots | grep pending | wc -l`
# Check if we have any pending snapshots at all.
if [ $command == "0" ]
then
echo "No snapshots are pending."
ec2-describe-snapshots
else
# Wait for the snapshot to finish.
while [ $command != "0" ]
do
# Communicate that we're waiting.
echo "There are $command snapshots waiting for completion."
sleep 5
# Re run the command.
command=`ec2-describe-snapshots | grep pending | wc -l`
done
# Snapshot has finished.
echo -e "\n"
echo "Snapshots are finished."
fi
此代码有时可以正常工作,有时它不能正常工作。它进入无限循环。我想做这样的事情我想检查ec2-describe-snapshot
的输出,如果snaphost处于挂起状态。如果是,它应该等到所有快照都完成。
ec2-describe-snapshots的输出是
SNAPSHOT snap-104ef62e vol-a8 completed 2013-12-12T05:38:28+0000 100% 109030037527 20 2013-12-12: Daily Backup for i-3ed09 (VolID:vol-aecbbcf8 InstID:i-3e2bfd09)
SNAPSHOT snap-1c4ef622 vol-f0 pending 2013-12-12T05:38:27+0000 100% 109030037527 10 2013-12-12: Daily Backup for i-260 (VolID:vol-f66a0 InstID:i-2601)
答案 0 :(得分:3)
如果至少有一个待处理快照,程序将永远循环。也许通过更改脚本来打印那些待处理的快照会很有帮助:
echo "There are $command snapshots waiting for completion."
ec2-describe-snapshots | grep pending
但肯定不是真的无限发生。你可能只需要等待。当没有更多待处理的快照时,循环将停止。真。
顺便说一句,这是一个略微改进的脚本版本。它等同于你的,只是改进了语法以删除一些不必要的东西并用现代方法替换旧式写作:
command=$(ec2-describe-snapshots | grep pending | wc -l)
# Check if we have any pending snapshots at all.
if [ $command = 0 ]
then
echo "No snapshots are pending."
ec2-describe-snapshots
else
# Wait for the snapshot to finish.
while [ $command != 0 ]
do
# Communicate that we're waiting.
echo "There are $command snapshots waiting for completion."
ec2-describe-snapshots | grep pending
sleep 5
# Re run the command.
command=$(ec2-describe-snapshots | grep pending | wc -l)
done
# Snapshot has finished.
echo
echo "Snapshots are finished."
fi