Linux concurrency mutex scripts

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?

+6
source share
3 answers

I am not a PHP programmer, but the documentation says that it provides a portable version of flock that you can use. The first sample snippet looks pretty close to what you want. Try the following:

 <?php $fp = fopen("/tmp/lock.txt", "r+"); if (flock($fp, LOCK_EX)) { // acquire an exclusive lock // Do your critical section here, while you hold the lock flock($fp, LOCK_UN); // release the lock } else { echo "Couldn't get the lock!"; } fclose($fp); ?> 

Note that, by default, flock waits until it can get a lock. You can use LOCK_EX | LOCK_NB LOCK_EX | LOCK_NB , if you want it to LOCK_EX | LOCK_NB immediately when another copy of the program is already running.

Using the name "/tmp/lock.txt" can be a security hole (I donโ€™t want to think seriously enough to decide if this is really), so you should probably choose a directory where you can write only your program.

+2
source

You can use flock to automatically lock the flag file. The -e option is for exclusive locking.

On the man page:

By default, if a lock cannot be obtained immediately, the pack waits until the lock is available.

So, if all your bash / php scripts try to lock the file exclusively, only one can successfully receive it, and the rest will wait for the lock.

If you do not want to wait thenuse -w before timeout.

+1
source

fuser - locking in Bash (this ensures that neither of the two processes gets access to the protected resource at the same time, but can lead to a negative attempt to block, even if no processes access the resource, almost unbelievable):

 #!/bin/bash set -eu function mutex { local file=$1 pid pids exec 8>>"$file" { pids=$(/sbin/fuser -f "$file"); } 2>&- 9>&- for pid in $pids; do [[ $pid = $$ ]] && continue exec 8>&- return 1 # locked by other pid done } 
0
source

All Articles