Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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":



    • 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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Domain Nodelet?
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this?Last hourOther CB clients
    Other Users?
    Others having an uproarious good time at the Monastery: (4)
    As of 2024-04-19 21:21 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found