Skip to content

Instantly share code, notes, and snippets.

@ahoward ahoward/a.rb
Created Dec 9, 2010

What would you like to do?
#! /usr/bin/env ruby
status = DATA.flock(File::LOCK_EX | File::LOCK_NB)
if status == 0
puts "we have the lock..."
puts "another process has the lock..."
### this data section MUST be here for DATA.flock to function.
### the user locking the script MUST have write privledges to the script

This comment has been minimized.

Copy link
Owner Author

commented Dec 9, 2010

this is a simple technique that portably prevents two copies of a script from running at the same time. if you have ruby scripts in your crontab you will burn in hell if you aren't using this or a similar technique to prevent script stacking taking your box down. same applies for background jobs.


This comment has been minimized.

Copy link

commented Dec 11, 2010

Great idea! Just today I wrote a backup script and made use of this technique.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.