我在 cron 中安排了 2 个 shell 脚本 a.sh 和 b.sh,其中第一个 a.sh 计划在早上 5 点运行,第二个脚本 b.sh 将在早上 7 点运行。
有先决条件声明:
1) Only one can run at a time.
2) The second script b.sh should run only after the first script a.sh completes.
现在的问题是第一个脚本 a.sh 可能会在早上 7 点之前完成执行,或者在某些情况下可能会超过早上 7 点,并且在超过早上 7 点的情况下,第二个脚本也会启动,这将打破其中一个先决条件。
这里我无法在当天晚些时候开始第二个脚本,因为它非常关键。
这里如何让两个脚本一个接一个地运行,而不是第一个脚本踩到第二个脚本。
最佳答案
让 a.sh 脚本在完成后创建一个锁定文件(最后一行)。
touch ~/a.lck
b.sh开头:
if [ ! -f ~/a.lck ]; then
exit 1 #or whatever you want it to do if the lock file is not there.
fi
不要忘记在 a.sh 的开头或 b.sh 的结尾 rm 锁定文件!
关于bash - 关于 shell 脚本调度的说明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20631143/