I have a bash deployment script that handles deploying updated code to a Tomcat instance on CentOS, however, both Chef and RunDeck may invoke the script, and since Chef runs periodically there is a chance of a collision.

How do I prevent the deployment script from running twice concurrently?

The standard answer looks to be to wrap the deploy logic in a flock.

However, since the deploy restarts tomcat that isn't working -- the new java process inherits the lock and prevents any further deploy scripts from executing.

Is there another way to prevent concurrent execution or a way to prevent flock inheritance?


You can close the file descriptor where flock maintains the lock before running the program that you want to run unlocked.

  flock -n 9 || exit 120
  (exec 9>&-; tomcat &)
) 9>/var/run/my.lock

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.