Synchronizing four shell scripts to run one after another in unix

前端 未结 3 656
慢半拍i
慢半拍i 2021-01-15 22:39

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.

3条回答
  •  不思量自难忘°
    2021-01-15 23:12

    You are experiencing a classical race condition. To solve this issue, you need a shared "lock" (or similar) between your 4 scripts.

    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
    

    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).

    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)

提交回复
热议问题