-
-
Save ahoward/766bf211f3b6f075f6ca to your computer and use it in GitHub Desktop.
make sure a ruby program can run only one copy of itself
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /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