#!/usr/bin/perl # # $Id: lockit.pl,v 1.3 2003/01/13 05:28:42 jmates Exp $ # Jeremy Mates # # Attempts to lock files. # # For documentation, use the -h option or run perldoc on this script. # # Distributed under the Artistic License: # http://sial.org/artistic_license.txt # ###################################################################### # # MODULES use Carp; use Getopt::Std; # file locking related modules use Fcntl qw(:DEFAULT :flock); use IO::Handle; use strict; use vars qw:$VERSION:; ###################################################################### # # CONSTANTS my (%opts); ($VERSION = '$Revision: 1.3 $ ') =~ s/[^0-9.]//g; ###################################################################### # # MAIN # parse command-line options getopts('h?z', \%opts); help() if exists($opts{'h'}) || exists($opts{'?'}); my $logfile = shift; # filename to lock # attempt lock on named file... # open & aquire write lock on the logfile open (FD, "+< $logfile") or die "Error opening $logfile: $!\n"; FD->autoflush(1); unless (flock (FD, LOCK_EX|LOCK_NB)) { warn "Waiting for write lock on $logfile ...\n"; flock (FD, LOCK_EX); } # zap the file if you want to... if (exists $opts{'z'}) { seek (FD, 0, 0) or die "Problem seeking $logfile: $!\n"; truncate (FD, 0) or die "Problem truncating $logfile: $!\n"; } sleep 60; flock (FD, LOCK_UN); close (FD); ###################################################################### # # SUBROUTINES # a generic help blarb sub help { print <<"HELP"; $0 v.$VERSION Usage: $0 [options] [LOGFILE] Purpose: simple file locker. Options: -h/-? Display this message -z Zap the file (empty contents thereof), too. Run perldoc on this script for more documentation. HELP exit; } __END__ =head1 NAME lockit.pl - simple file locker. =head1 SYNOPSIS Pretty simple, just run: $ lockit.pl =head1 DESCRIPTION Locks files, or gives it a good shot, anyways. Sleeps for 60 seconds, probably enough for you to test stuff. =head1 USAGE $ lockit.pl [options] LOCKFILE =head1 OPTIONS lockit.pl supports the following command line options: -h Get a brief help blarb -z Zap the file. Truncates the file down the 0 bytes. =head1 LICENSE This script is distributed under the terms of the Artistic License: http://sial.org/artistic_license.txt =head1 BUGS Lots, probably. =head1 SEE ALSO flock(2), perl(1) =head1 AUTHOR Jeremy Mates, jmates@sial.org =cut