Home > Software design >  Example of Using Flock in CURL to Avoid Multiple Parallel Instances
Example of Using Flock in CURL to Avoid Multiple Parallel Instances

Time:06-18

my bash script runs using incron whenever there is change in directory. However, since multiple images are uploaded, so I don't want to run multiple instances of CURL (that is used to build thumbnail cache of those images) while images are being moved to different directory. Running multiple instances of CURL results in building flawed thumbnails of images.

Flawed Thumbnail Example Good Thumbnail Example

I am not an expert here but I heard flock can lock the process, can i use flock to lock the CURL and unless its free, there should not be multiple instances of CURL in parallel (For instance could i add something like curl --silent http://127.0.0.1/build-cache.php

pi@pi:~$incrontab -e
/var/www/html/images/    IN_CLOSE_WRITE  /usr/local/bin/SERVIES.sh
pi@pi:~$sudo nano /usr/local/bin/SERVIES.sh
#!/bin/bash
find '/var/www/html/images' -maxdepth 1 -name '*jpg' -exec bash -c 'mv {} /var/www/html/gallery/$(basename {} | sed "s/^.\{,19\}//")' \; && 
curl --silent http://127.0.0.1/build-cache.php

CodePudding user response:

It was pretty easy though!

pi@pi:~$sudo nano /usr/local/bin/SERVIES.sh
#!/bin/bash
find '/var/www/html/images' -maxdepth 1 -name '*jpg' -exec bash -c 'mv {} /var/www/html/gallery/$(basename {} | sed "s/^.\{,19\}//")' \; && 
flock -n /var/www/html/build-cache.lock curl --silent curl --silent http://127.0.0.1/build-cache.php
  • Related