On my Linux server, I need to synchronize several scripts written in BASH and PHP, so that only one of them can run a critical task, which is a series of BASH / PHP commands that can ruin things if they are executed simultaneously by two or more scripts. From my experience with multithreading in C ++, I am familiar with a mutex, but how to implement a mutex for a bunch of scripts that run in separate processes and, of course, are not written in C ++?
Well, the first solution that comes to mind will be for each of the scripts to initially create a โlock flagโ file, so that other scripts know that the job is โlockedโ, and then deletes the file after it completes. But, as I see it, file write and read operations must be completely atomic in order for this approach to work with a 100 percent probability, and the same requirement applies to any other synchronization method. And I am sure that the write / read operations of files are not atomic, they are not atomic in all existing Linux / Unix systems.
So what is the most flexible and reliable way to synchronize parallel BASH and PHP scripts?
source share