Skip to content

Instantly share code, notes, and snippets.

@samg
Last active September 28, 2015 11:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samg/1430991 to your computer and use it in GitHub Desktop.
Save samg/1430991 to your computer and use it in GitHub Desktop.
Implement a lockfile in ruby
require 'fileutils'
begin
path_to_lock_file = '/tmp/lock.lock'
File.new(path_to_lock_file, File::CREAT|File::EXCL) # raise if file already exits
begin
system *ARGV
ensure
FileUtils.rm path_to_lock_file
end
rescue Errno::EEXIST
warn "Job currently running, exiting."
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment