Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: RFC: A new module to help avoid running multiple instances of the same script (via cron, for example)

by scorpio17 (Canon)
on Dec 03, 2009 at 22:07 UTC ( [id://810965]=note: print w/replies, xml ) Need Help??


in reply to RFC: A new module to help avoid running multiple instances of the same script (via cron, for example)

Thanks for the feedback, everyone.

Here's my revised version:

package HighLander; use strict; use Fcntl qw( LOCK_EX LOCK_NB ); open(our $fh, '<', $0) or die("Can't open \"$0\": $!\n"); unless ( flock($fh, LOCK_EX|LOCK_NB )) { print "Another instance of \"$0\" is already running. Exiting...\n"; exit(0); } 1;

Here's an example of something using it:

#!/usr/bin/perl use strict; use HighLander; # if this script is already running, you'll never make it this far... print "Running...\n"; sleep 10;

A few notes:
I like using flock much better than creating a pid file. But creating a lock file is just as annoying. Getting a lock on the script itself is brilliant! When the process goes away, so does the lock, so the issue with bogus pid files lying around after interrupted jobs goes away, too. I've tested this on RedHat linux (RHEL5). I've also tried it using a run of the real script and a run with a symbolic link - and it does the right thing in that case also. On WinXP, using perl v5.8.7 (ActiveState build 813), it seems to work - sort of - the second job exits, but I don't see the "Another instance is already running" message. But the original intent was to use this with cron, which doesn't apply to WinXP anyway. Win32::Mutex is probably more appropriate for Windows users. And if you really need the process id, Proc::Pidfile seems like a better choice.

ikegami: I don't understand this line from your code:

if ($! == ($^O =~ /Win32/ ? 33 : EWOULDBLOCK)) {

This tests for an error condition, after getting a lock, and if you're running on Windows... 33?! It seems to work okay without this check, but I'd like to understand this better, for future reference.

Also - it seems like the file handle ($fh) needs to be a global variable, so I used 'our' instead of 'my'. If I use 'my', then when the package goes out of scope, the lock gets dropped!

mortiz: I'd like to add something like this:

use Highlander qw( :verbose );
To turn on the print statement. Something like this:
... unless ( flock($fh, LOCK_EX|LOCK_NB )) { if ($verbose) { print "Another instance of \"$0\" is already running. Exiting...\n +"; } exit(0); } ...

... but I don't see how to accomplish this using only an export tag. Any hints?

I'm not planning to upload this to CPAN. It seems too simple to bother. Besides, merlyn solved this problem over 9 years ago! He should get the credit, not me.

Thanks again for the helpful comments, everyone.

  • Comment on Re: RFC: A new module to help avoid running multiple instances of the same script (via cron, for example)
  • Select or Download Code

Replies are listed 'Best First'.
Re^2: RFC: A new module to help avoid running multiple instances of the same script (via cron, for example)
by ikegami (Patriarch) on Dec 03, 2009 at 23:06 UTC

    I don't understand this line from your code

    It checks the reason for why flock failed instead of assuming it's because the file is already locked.

    flock returns error EWOULDBLOCK on unixy systems and error 33 on Windows.

    it seems like the file handle ($fh) needs to be a global variable, so I used 'our' instead of 'my'. If I use 'my'

    Correct. In my version, the END sub kept it alive.

    I'd like to add something like this: use Highlander qw( :verbose );

    First, :name traditionally has a meaning already. Dashes are usually used for options. As a bonus, -foo means the same thing as '-foo' even when strict is in use, so less quoting is needed.

    Secondly, suppressing the message by default is a bad idea. The option should silent the message when provided.

    On to the good stuff,

    use Highlander -silent;
    is the same as
    BEGIN { require Highlander; Highlander->import(-silent); }

    so you need to create a method called import that looks like

    my $opt_silent; sub import { my $class = shift; $opt_silent = 0; for (@_) { if ($_ eq -silent) { $opt_silent = 1; } else { require Carp; Carp::carp("Unrecognized symbol $_"); } } }

    But it won't work. The problem is that the module has already been executed (and the lock obtained) by require before import is called.

    But you know what? The option isn't really needed. The message can always be suppressed on the command line by using output redirection.

    I'm not planning to upload this to CPAN. It seems too simple to bother.

    Yet you had to write it and needed help to do so. You can also credit whoever you want in the docs.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2024-04-23 20:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found