Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

up vote 0 down vote accepted

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
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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