Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Hola Monks,

Last week, I asked how to ensure that my Perl script wouldn't run if another instance of the same script was running. (Post was: "Mechanism for ensuring only one instance of a Perl script can only run?", Dec 2 2022) The solution that you guys suggested recommended taking advantage of flock() as a mechanism. (Thank you!!!) The basic implementation idea was simple: It my script ran and did not see an empty "lock file" in the local directory, it would create one and then flock() that file to lock it. Upon exiting, it would delete the file. If another instance of the same script ran and saw the lock file, it would immediately terminate. This was the "There Can Be Only One" (Highlander) approach.

I implemented this idea, but ran into management issues with the lock file. (The lock file was not deleted properly, it was overwritten, etc.) So I went back to the drawing board.

What do you think of this approach:

#!/usr/bin/perl use warnings; use strict; package main; my $name = `basename "$0"`; # Get this script's name $name =~ s/\R//g; # Chop off trailing newlines print "Script name is: \"$name\".\n"; my $dirPerl = "/usr/bin/perl"; my $psCmd = "ps -ef | grep $name"; my $scriptRunningCount = 0; my $output = qx($psCmd); # Run "ps -ef | grep SCRIPTNAME" foreach my $line (split /[\r\n]+/, $output) { if( index($line,$dirPerl) != -1 && index($line,$name) != -1 +){ $scriptRunningCount++; } } if($scriptRunningCount == 1){ # script... } print "END OF PROGRAM.\n";

This code literally does what I would do; it issues a `ps -ef` and makes sure it is the only script of its instance running. No flock() command, no managing an external file.

I'm still a Perl newbie. To me, the above is simpler... perhaps too simple. You guys are the experts, so I'm (humbly) asking for a Pros-Vs-Cons critique of this idea. If I only want one instance of my script to run, is the above code a solid approach? All criticisms are welcome. Thank you!

In reply to Ensure Only One Instance of Your Script is Running... with 'ps -ef' ? by redapplesonly

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, 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, 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.
  • Log In?

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

    How do I use this?Last hourOther CB clients
    Other Users?
    Others imbibing at the Monastery: (2)
    As of 2024-07-15 21:45 GMT
    Find Nodes?
      Voting Booth?

      No recent polls found

      erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.