[Canberra-pm] How to flock a file for to ensure only one CGI BIN script process at a time

John.Hockaday@ga.gov.au John.Hockaday at ga.gov.au
Thu Jul 28 17:34:34 PDT 2005


Hi,

I have a CGI-BIN script that chews up a lot of CPU and takes quite a while to
run.  I only want a select few users to run it one at a time.  It isn't
likely that users will want to run it very often.  Once every three months is
the expected frequency. I have put the script in an .httpaccess area that
only these users can get to.

The processSite sub routine is the bit that uses up CPU time.  printBusy just
tells the user to try again later because someone else is testing their site.

I thought that flock would be the best method to use a lock file to stop any
other users from  running the script while someone else has it running.  The
trouble is that the script creates the lockFile and doesn't delete it.  How
can I get flock to check if the lockFile exists and if so send a busy message
to CGI.  Otherwise, set the lockFile, flock it and process the site?

Any help would be greatly appreciated.

Thanks.


John

This is part of the code that I tried:

use CGI;
use Fcntl qw(:DEFAULT :flock);
my ($query) = new CGI;
	
	# If there is a zserver parameter then try to process the site.
if ($query->param('zserver')) {
  $zserver = $query->param('zserver');
  $port = $query->param('port');
  $repository = $query->param('repository');
  $title = $query->param('title');
  $title = '"' . $title . '"';
  # Check flock if flock send sorry being used wait
  # else set flock
  flock(LOCKFILE, LOCK_SH);
  sysopen(LOCKFILE, $lockFile, O_EXCL | O_CREAT) or &printBusy;
  &processSite();
  # unlock flock
  close LOCKFILE;
  undef $lockFile;
  exit (0);

	# Else print the HTML input form.
} else {
  &printInputPage;
  print "Loaded input page <br /> \n" if $DEBUG;
  exit (0);
}

sub printBusy {

  my ($q) = new CGI;

  print $q->header;
  print html_util::process_html($head);
  print html_util::process_html($busy);
  print html_util::process_html($foot);
  undef $q;
  exit (0);

} #End printBusy


More information about the Canberra-pm mailing list