> -----Original Message-----
> From: Theuerkorn Johannes [mailto:[EMAIL PROTECTED]] 
> Sent: 30 September 2002 10:18
> To: '[EMAIL PROTECTED]'
> Subject: how to know weather perl script is already running?
> 
> 
> Hello List,
> 
> i have a perl script that i have running from a cron Job. It 
> usually opens every 4 hours. But sometimes it takes the 
> script longer to execute its job so after cron opens up the 
> perl script as usual, i have two perl jobs running.
> Is there any possibility to check weather the script is 
> already running? Maybe i can use a system command in the crontab?
> 
> any help welcoe 
> 
> greets Johannes
> 
> -- 

We use the following subs to do this on Linux:
example call:

# In your main program:
# This will exit 0 if there are no available instances
my $instance = getInstance("reporter",3); # maximum of 3 reporters



#
------------------------------------------------------------------------
-----
# Get an instance name and lock
# Used to control the maximum number of instances for a specified
process
# e.g.  reporter.pl
sub getInstance {
  my $name  = shift;
  my $max   = shift;
  my $msg   = '';

  $max ||= 1;
  $msg ||= "No available instances for '$name' [maximum $max already
locked]";

  my $locked = '';
  foreach my $instance ( 1..$max ) {
    my $token = "$name$instance";
    $locked = $token if lockToken($token);
    last if $locked;
  }

  # Note that we die gracefully
  print "\n$msg\n"  unless $locked;
  exit 0 unless $locked;

  return $locked;
}
#
------------------------------------------------------------------------
-----
sub lockToken {
  my $token = shift;

  mkdir "/var/lock" unless -d "/var/lock";
  mkdir "/var/lock/mylocks" unless -d "/var/lock/mylocks";

  my $token_file = "/var/lock/mylocks/$token";
  open(FH, ">$token_file") ||
    LogFatal("Cannot open token locking file '$token_file' for writing:
$OS_ERROR");

  # Return success or failure depending on whether we can exclusively
lock the file
  return flock(FH, LOCK_EX | LOCK_NB);

}



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to