Skip to content

Instantly share code, notes, and snippets.

@mitchellh
Created October 26, 2011 17:55
Show Gist options
  • Save mitchellh/1317151 to your computer and use it in GitHub Desktop.
Save mitchellh/1317151 to your computer and use it in GitHub Desktop.
Chef file lock to enforce only one chef run
# Putting this at the top of your "base" recipe or some common recipe that
# is loaded very early on will force Chef to only run one process at a time.
# This is enforced using a file lock. The file lock is automatically released
# when the process exits, so there is no need to explicitly do so.
#----------------------------------------------------------------------
# Obtain File Lock
#----------------------------------------------------------------------
$_chef_lock_file = File.open(File.join(Chef::Config[:file_cache_path], "chef_lock"), "w+")
if $_chef_lock_file.flock(File::LOCK_EX | File::LOCK_NB) === false
Chef::Log.info("Another Chef process running. Not running.")
exit(true)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment