Skip to content

Instantly share code, notes, and snippets.

@waffle2k
Created November 13, 2009 18:40
Show Gist options
  • Save waffle2k/234059 to your computer and use it in GitHub Desktop.
Save waffle2k/234059 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl -w
use strict;
use Fcntl qw(:DEFAULT :flock );
use Getopt::Std;
sub usage {
print <<USAGE
Usage:
SCRIPT.pl -f LOCKFILE
-l: The Filename which you wish to lock
USAGE
;
exit(0);
}
my %options;
getopts("luhf:",\%options);
usage() if $options{h};
usage() unless defined $options{f};
sysopen( FD, "options{f}", O_RDWR|O_EXCL, 0755)
or die("Cannot open $options{f}: $!\n" );
unless ( flock ( FD, LOCK_EX | LOCK_NB ) ) {
local $| = 1;
print "Waiting for lock on filename...";
flock ( FD, LOCK_SH ) or die ( "Can't lock file: $!\n" );
print "\n";
} ## end unless ( flock ( FD, LOCK_EX...
print "Received it! Hit Enter to release it...";
my $r = <>;
close ( FD );
exit( 0 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment