Skip to content

Instantly share code, notes, and snippets.

@ahoward
Created August 5, 2014 15:59
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 ahoward/766bf211f3b6f075f6ca to your computer and use it in GitHub Desktop.
Save ahoward/766bf211f3b6f075f6ca to your computer and use it in GitHub Desktop.
make sure a ruby program can run only one copy of itself
#! /usr/bin/env ruby
# it's trivial ot make a ruby program run once.
#
status = DATA.flock(File::LOCK_EX|File::LOCK_NB)
unless status == 0
puts "another process has the lock"
exit(42)
else
puts Process.pid
puts "holding the lock until you hit ctrl-c..."
sleep
end
__END__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment