同步四个Shell脚本以在UNIX中一个接一个地运行
我有4个Shell脚本来生成一个文件(比如说param.txt),该文件将由另一个工具(informatica)使用,并且随着该工具的处理完成,它将删除param.txt.
I have 4 shell script to generate a file(let's say param.txt) which is used by another tool(informatica) and as the tool is done with processing, it deletes param.txt.
这里的意图是所有四个脚本可以在不同的时间被调用,比如说12:10 am,12:13 am,12:16 am,12:17 am.第一个脚本在上午12:10运行,并创建param.txt并触发使用param.txt的informatica进程. Informatica流程又需要5到10分钟才能完成,并删除param.txt.第2个脚本在上午12:13调用并等待param.txt的不可用,并且随着informatica进程将其删除,脚本2创建了新的param.txt并再次触发相同的informatica.另外两个脚本也会发生同样的情况.
The intent here is all four scripts can get invoked at different time lets say 12:10 am, 12:13 am, 12:16 am, 12:17 am. First script runs at 12:10am and creates param.txt and trigger informatica process which uses param.txt. Informatica process takes another 5-10 minutes to complete and deletes the param.txt. The 2nd script invokes at 12:13 am and waits for unavailability of param.txt and as informatica process deletes it, script 2 creates new param.txt and triggers same informatica again. The same happen for another 2 scripts.
我正在所有4个shell脚本中使用直到和睡眠命令来检查param.txt的不可用性,如下所示:
I am using Until and sleep commands in all 4 shell script to check the unavailability of param.txt like below:
until [ ! -f "$paramfile" ]
do
Sleep 10
done
<create param.txt file>
这里的问题是,有时当所有4个脚本开始时,第一个成功并生成param.txt(因为之前没有param.txt)和其他等待,但是当informatica进程完成并删除param.txt时,剩下的3个脚本或其中两个同时检查不可用性,其中一个会创建不可用性,但所有操作都会成功.我检查了四个脚本之间不同的睡眠间隔组合,但是这种情况几乎每次都发生.
Issue here is, sometimes when all 4 scripts begin, the first one succeeds and generates param.txt(as there was no param.txt before) and other waits but when informatica process completes and deletes param.txt, remaining 3 scripts or 2 of them checks the unavailability at same time and one of them creates it but all succeed. I have checked different combinations of sleep interval between four scripts but this situation is occurring almost every time.
您正在经历经典的比赛条件.要解决此问题,您需要在4个脚本之间共享一个锁"(或类似名称).
You are experiencing a classical race condition. To solve this issue, you need a shared "lock" (or similar) between your 4 scripts.
有几种方法可以实现此目的.在bash中执行此操作的一种方法是使用flock
命令,并使用同意的文件名作为锁定. flock手册页具有一些类似于以下的用法示例:
There are several ways to implement this. One way to do this in bash is by using the flock
command, and an agreed-upon filename to use as a lock. The flock man page has some usage examples which resemble this:
(
flock -x 200 # try to acquire an exclusive lock on the file
# do whatever check you want. You are guaranteed to be the only one
# holding the lock
if [ -f "$paramfile" ]; then
# do something
fi
) 200>/tmp/lock-life-for-all-scripts
# The lock is automatically released when the above block is exited
您也可以要求flock
如果无法获取锁,则立即失败,或者在超时后失败(例如,打印仍在尝试获取锁"并重新启动).
You can also ask flock
to fail right away if the lock can't be acquired, or to fail after a timeout (e.g. to print "still trying to acquire the lock" and restart).
根据您的用例,您还可以将锁锁定在"informatica"二进制文件上(在这种情况下,请确保使用200<
来打开文件以供读取(而不是(过度)写入)
Depending on your use case, you could also put the lock on the 'informatica' binary (be sure to use 200<
in that case, to open the file for reading instead of (over)writing)