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?

    In reply to Conditional Lock File by bichonfrise74

    Title:
    Use:  <p> text here (a paragraph) </p>
    and:  <code> code here </code>
    to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.