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?
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|