I would like to hear some advise on how to deal with this scenario. My goal or what I think I want to happen is this:
Run a script without arguments (main script) if the same script is not running.
Run a script with argument while the main script is running but not if there is a script with argument is running.
Here is the code.
#!/usr/bin/perl
use strict;
use Fcntl qw( :flock );
use Getopt::Long;
GetOptions( 'test' => \my $option_test );
print "Running $0\n";
check_process() if ( ! defined( $option_test ) );
sleep 30;
print "Running $0\n";
sub check_process {
open( SELF, '<', $0 )
|| die "Error: Cannot open $0 for locking - $!\n";
flock( SELF, LOCK_EX | LOCK_NB )
|| die "Error: Another $0 is already running.\n";
return 1;
}
So, here is the flow that I want.
$ perl test.pl --> should run if there is no test.pl running.
$ perl test.pl ---> should abort if there is a test.pl running alread
+y.
$ perl test.pl --test --> should run even if test.pl is already runni
+ng.
$ perl test.pl --test --> should abort if there is a (test.pl --test)
+ running as well.
So, I am thinking of just creating a new script for (test.pl --test), but the reason why I am hesitant to this is because the new script is almost 95% identical to the main script.
Would this be the best course of action for this scenario?