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?

1 Answer 1


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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .