I'm the person that posed the question to beppu ;)

Thanks for the great answers, I'm glad I finally made it on to perlmonks!

So... Proc::Podfile didn't work out for me too well. I couldn't seem to get it working right. I might have been using it wrong, or perhaps I just didn't understand how to use it from reading the docs.

Then I found File::Pid, and I'm very happy. Unfortunately, it is not smart enough to release the pidfile if the script crashes, but that's not a big deal for my current needs.

Here was the implementation I used. I decided to allow my script to get a --force flag, in case it was really important that the script run at a certain time (even if a crash happened and there's a pid file hanging around). Under normal circumstances, the script will exit if another instance is discovered.

use File::Pid; # Check to make sure we are not already running my $pidfile = File::Pid->new({file => "/path/to/my.pid"}); # This next line gets the current pid in $pidfile # If nothing was running, we should get back our own pid my $pid = $pidfile->write; # now, die if the pid file cannot be opened, # or the pid running is not THIS INSTANCE # note: this can be overridden if $FORCE is true. die ("running process=".$pid.", my pid=$$\n") if (!$pid or ($pid != $$ + and !$FORCE)); # ... do a bunch of stuff for a long time $pidfile->remove or warn "Couldn't unlink pid file\n";
somebox

In reply to Re^2: Cron Jobs That Run For Too Long by somebox
in thread Cron Jobs That Run For Too Long by beppu

Title:
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, details, 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, summary, 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.