I want to run multiple Bash shell scripts in parallel. However, I want to avoid race conditions. What Unix commands are truly atomic that I could use for this purpose, and how can I use them?
|
If
|
|||
|
|
This ensures that code between "(" and ")" is run only by one process at a time and that the process does wait for a lock too long. |
|||||||
|
|
lockfile(1) looks like a good candidate, though beware that it's part of the procmail package, which you may not have installed on your machine yet. It's a popular enough package that it should be packaged for your system if it's not installed yet. Three of the four systems I checked have it, and the other has it available. Using it is simple:
The options I've given make it retry once a second for up to 15 seconds. Drop the "-r" flag if you want it to wait forever. |
|||||
|
|
The system call Caveat emptor: |
||||
|
|
|
Maybe the lockfile command will do what you need.
|
||||
|
|
make(1)to take over? (i.e., do amake -j 9if you have 8 cores)? This has the added advantage of interleaving work with finer granularity. – vonbrand Mar 15 at 14:50