Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^2: Cron Jobs That Run For Too Long

by ruoso (Curate)
on Dec 20, 2005 at 22:07 UTC ( #518197=note: print w/replies, xml ) Need Help??


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

Just a note, adding to what sgifford said...

I've seen some scripts around listing /proc to check if the process is still running. I prefer to use a kill 0 on the proccess...

And I, personally, prefer the second way because it can be used for lockfiles even if the only place you can write the lockfile can't provide locking, or the locking isn't reliable (NFS)

daniel

Replies are listed 'Best First'.
Re^3: Cron Jobs That Run For Too Long
by adamk (Chaplain) on Dec 20, 2005 at 23:22 UTC
    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!

      (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.

      hey, that's really nice.
      this works great!!! can you give explenations on the __DATA__ part? I dont have a clue what is this for... Roy
      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://518197]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2022-05-21 20:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (78 votes). Check out past polls.

    Notices?