On Tue, Feb 23, 2016 at 06:34:54PM -0800, Kenneth Wolcott wrote:

 But where I run into trouble is the actual glob or readdir as $0
might be "./script1.pl" or "/full/path/to/script1.pl" or "script1.pl".
Perhaps I should use basename?  Perhaps a simple regex?

 I tried something similar with glob, but that didn't work well either.

 Here's the concept I tried and it doesn't seem to work right:

#!/usr/bin/perl

use strict;
use warnings;
use English qw( -no_match_vars );

my $cmd;
my $results;
my $lock_dir = $ENV{'HOME'} . "/My_build_lock_dir";
if (-d $lock_dir) {
 opendir my $dh, $lock_dir or die "Cannot open $lock_dir: $!";
 foreach my $file (readdir $dh) {
   next if ($file eq ".");
   next if ($file eq "..");
   die "FATAL ERROR: $file is currently executing, cannot continue
executing $PROGRAM_NAME\n" if ($file eq "script1.pl");
   die "FATAL ERROR: $file is currently executing, cannot continue
executing $PROGRAM_NAME\n" if ($file eq "script2.pl");
   die "FATAL ERROR: $file is currently executing, cannot continue
executing $PROGRAM_NAME\n" if ($file eq "script3.pl");
   die "FATAL ERROR: $file is currently executing, cannot continue
executing $PROGRAM_NAME\n" if ($file eq "script4.pl");
   die "FATAL ERROR: $file is currently executing, cannot continue
executing $PROGRAM_NAME\n" if ($file eq "script5.pl");
 }
 closedir $dh or warn "Unable to close $lock_dir: $!";
} else {
 mkdir $lock_dir;
}
$cmd = "cat $PID > $lock_dir/$PROGRAM_NAME";
$results = qx{$cmd};
print "Added $PROGRAM_NAME to $lock_dir\n";
print "Sleeping for 60 seconds\n";
sleep 60;
unlink "$lock_dir/$PROGRAM_NAME" or die "Failed to remove lock file
($lock_dir/$PROGRAM_NAME); remove manually.\n";
print "Done!\n";



I tried with the below modified script. It works. See if this is
something you can use.

#!/usr/bin/perl

use strict;
use warnings;
use English qw( -no_match_vars );
use File::Basename;

my $PROGRAM_NAME= basename($0);

my $cmd;
my $lock_dir = $ENV{'HOME'} . "/My_build_lock_dir";
if (-d $lock_dir) {
 opendir my $dh, $lock_dir or die "Cannot open $lock_dir: $!";
 foreach my $file (readdir $dh) {
   next if ($file eq ".");
   next if ($file eq "..");
   die "FATAL ERROR: $file is currently executing, cannot continue
executing $PROGRAM_NAME\n" if ($file eq "script1.pl");
   die "FATAL ERROR: $file is currently executing, cannot continue
executing $PROGRAM_NAME\n" if ($file eq "script2.pl");
   die "FATAL ERROR: $file is currently executing, cannot continue
executing $PROGRAM_NAME\n" if ($file eq "script3.pl");
   die "FATAL ERROR: $file is currently executing, cannot continue
executing $PROGRAM_NAME\n" if ($file eq "script4.pl");
   die "FATAL ERROR: $file is currently executing, cannot continue
executing $PROGRAM_NAME\n" if ($file eq "script5.pl");
 }
 closedir $dh or warn "Unable to close $lock_dir: $!";
} else {
 mkdir $lock_dir;
}
$cmd = `echo $PID > $lock_dir/$PROGRAM_NAME`;
print "Added $PROGRAM_NAME to $lock_dir\n";
print "Sleeping for 60 seconds\n";
sleep 60;
unlink "$lock_dir/$PROGRAM_NAME" or die "Failed to remove lock file
($lock_dir/$PROGRAM_NAME); remove manually.\n";
print "Done!\n";


--
Atnakus Arzah <atnakus.ar...@gmail.com>

When in doubt, have a cookie!

Attachment: signature.asc
Description: PGP signature

Reply via email to