linux - 如何在代码块之间插入等待

标签 linux bash amazon-ec2

我想实现这样的功能:我正在创建卷快照(使用亚马逊 ec2 工具),并将最新快照的详细信息存储在文件中。

#Create snapshots of all attached volumes

echo "Create snapshots of all attached volumes"
read -rsp $'Press enter to continue...\n'
awk '{print $2, $3}' "$EC2_HOME/ActiveVolumes_$today_date" | while read vol_id inst_id; do
    awk '{print $3, $5}' "$EC2_HOME/Instances_$today_date" | while read inst_id2 name; do
        if test "$inst_id" = "$inst_id2"; then
            echo ec2-create-snapshot "$vol_id" -d "$today_date: Daily Backup for $inst_id (VolID:$vol_id InstID:$inst_id)"
             ec2-create-snapshot "$vol_id" -d "$today_date: Daily Backup for $inst_id (VolID:$vol_id InstID:$inst_id)"
        fi
    done
done


#Create a file with all latest snapshots

echo "Create a file with all latest snapshots"
read -rsp $'Press enter to continue...\n'
latestdate=$(ec2-describe-snapshots | grep ^SNAPSHOT | sort -rk 5 | awk '{print substr($5, 1, 10); exit}')
ec2-describe-snapshots | grep "^SNAPSHOT.*$latestdate" > "$EC2_HOME/SnapshotsLatest_$today_date"

我想做一件事。我想根据一个条件在两个代码块之间等待。我想在执行第一个代码块后等待。我想检查状态是否已完成然后执行下一个代码块。

SNAPSHOT    snap-7749   vol-86d0    pending 2013-12-11T04:17:57+0000    100%    109030037527    35  EBS_Automated_Snapshot_12-10-2013-20:20:13
SNAPSHOT    snap-e2f3dc vol-80  completed   2013-12-11T04:16:49+0000    100%    109030037527    35  EBS_Automated_Snapshot_12-10-2013-20:19:05

最佳答案

To check if status of is completed then execute next block of code.

您还可以使用“; && ||”之一这 ;只是将一个命令与另一个命令分开。 && 表示仅在前一个命令成功的情况下才运行以下命令” || - 如果不成功。 Examples

如果还不够,还有 sleep 和等待命令:

  • 等待:wait、waitpid、waitid - 等待进程更改状态
  • sleep :延迟指定的时间

To put a wait after the first block of code is executed. Just use sleep num.

echo "hello"
# wait 5 seconds
sleep 5 
echo  "Bye" 

更多信息:男人 sleep 。

关于linux - 如何在代码块之间插入等待,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20513439/

相关文章:

LINUX - shell 脚本查找并列出所有有权在目录树中写入的文件

linux - 如何让MongoDB的mongorestore更新和替换具有相同_id的文件

linux - Mongo 输出到 shell 变量

bash - 使用 Bash 获取最后一次出现的正则表达式模式匹配

bash - 使用ssh命令传递环境变量

amazon-ec2 - N 集群中的 Cassandra N 副本?

node.js - 如何使用 .ebextensions 在 Amazon Elastic Beanstalk 中运行 `npm install`

从 EC2 中的 MySQL 5.7 到 AWS RDS 的 mysqldump

c++ - 使用链接 linux 编译 GLFW 应用程序问题

linux - 如何在日志文件中查找包含特定单词的行?