在 shell 脚本将同步 cmd 等待直到第一个 cmd 完全执行

In shell script will sync cmd wait until first cmd is executed completely

在 shell 脚本中,第二个 cmd 将等待第一个 cmd 完全执行,即在同步文件夹的情况下?

例如:

cmd1 sync folder1 
cmd2 cp folder1 to folder2

这里会同步完成后cmd2 运行?如果同步失败怎么办?

来自http://linux.die.net/man/8/sync

On Linux, sync is only guaranteed to schedule the dirty blocks for writing; it can actually take a short time before all the blocks are finally written. The reboot(8) and halt(8) commands take this into account by sleeping for a few seconds after calling sync(2).

(我强调的。)您可能也应该这样做。显然,必要的等待时间没有上限,也没有办法(至少对简单用户而言)无法验证磁盘是否已处理排队的命令,这有点麻烦。

当然这个问题对于sync之外的各种命令都存在。邮件可能尚未发送,打印作业可能仍在排队等。此外,正如 chepner 所说,任何分叉服务器或其他后台进程的命令(隐含地,没有 &)将 return 而进程还在运行.