Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^3: Cron Jobs That Run For Too Long

by adamk (Chaplain)
on Dec 20, 2005 at 23:22 UTC ( [id://518209]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Cron Jobs That Run For Too Long
in thread Cron Jobs That Run For Too Long

But wait, it gets better again!

You know how you can have that nifty __DATA__ block at the end of your script? It turns out you can lock that too :)

I've used this a number of times and it works just great.

#!/usr/bin/perl

use strict;
use Fcntl 'LOCK_EX', 'LOCK_NB';

unless ( flock DATA, LOCK_EX | LOCK_NB ) {
    print STDERR "Found duplicate script run. Stopping\n";
    exit(0);
}

...

1;





### DO NOT REMOVE THE FOLLOWING LINES ###

__DATA__
This exists to allow the locking code at the beginning of the file to work.
DO NOT REMOVE THESE LINES!

Replies are listed 'Best First'.
Re^4: Cron Jobs That Run For Too Long
by LanX (Saint) on Apr 23, 2021 at 16:02 UTC
    (For the record if someone reads this thread again)

    Cool trick, but strongly discouraged on Windows.

    See Re^2: File lock demo and other links in that thread for explanation.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

      but strongly discouraged on Windows

      Good point...
      But the OP did mention CRON and not Task Scheduler.

        This trick was referenced many times in the following years, and many people won't bother to read the OP.

        And personally I take "cron" as a generic term and I've also already seen people installing a cron demon on Win.

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery

Re^4: Cron Jobs That Run For Too Long
by astroboy (Chaplain) on Dec 23, 2005 at 18:40 UTC
    hey, that's really nice.
Re^4: Cron Jobs That Run For Too Long
by Anonymous Monk on Jul 12, 2007 at 15:36 UTC
    this works great!!! can you give explenations on the __DATA__ part? I dont have a clue what is this for... Roy
Re^4: Cron Jobs That Run For Too Long
by Anonymous Monk on Oct 21, 2016 at 19:08 UTC
    11 years later this is still brilliant.

Log In?
Username:
Password:

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

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

    No recent polls found