Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re^2: Mechanism for ensuring only one instance of a Perl script can only run?

by redapplesonly (Sexton)
on Dec 02, 2022 at 17:04 UTC ( [id://11148506]=note: print w/replies, xml ) Need Help??


in reply to Re: Mechanism for ensuring only one instance of a Perl script can only run?
in thread Mechanism for ensuring only one instance of a Perl script can only run?

Thanks Corion! So one caveat I have is that if a newly-executed script realizes that there's another instance already at work, I want the new script to terminate immediately, not enter a queue to run later. It looks like flock() doesn't offer a "terminate immediately" option... or am I missing the bigger picture. Thank you so much for writing.
  • Comment on Re^2: Mechanism for ensuring only one instance of a Perl script can only run?
  • Download Code

Replies are listed 'Best First'.
Re^3: Mechanism for ensuring only one instance of a Perl script can only run?
by Corion (Patriarch) on Dec 02, 2022 at 17:43 UTC

    No. flock returns a false value if it cannot lock a file because it is already locked. The idea is that you then exit your program:

    use Fcntl qw(LOCK_EX LOCK_NB); my $scriptname = $0; # assuming that all script instances will be invo +ked the same way open my $script, $scriptname or die "Couldn't find myself?! Looked for '$script', but got $!"; if( !flock $script, LOCK_EX | LOCK_NB ) { print "We are already running\n"; exit 1; }; sleep 60

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11148506]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (4)
As of 2024-04-19 06:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found