Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Cron Jobs That Run For Too Long

by perrin (Chancellor)
on Dec 20, 2005 at 20:27 UTC ( [id://518178]=note: print w/replies, xml ) Need Help??


in reply to Cron Jobs That Run For Too Long

See Proc::Pidfile. There are several similar modules on CPAN as well.

Replies are listed 'Best First'.
Re^2: Cron Jobs That Run For Too Long
by somebox (Initiate) on Jan 11, 2006 at 06:33 UTC
    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
      What what it's worth, I'd like to endorse the solution offered by adamk earlier in the thread. I had been handling a similar problem through the use of lockfiles, but it wasn't working out too well. I decided to give adamk's suggestion a try and it absolutely works a treat. It's also nice and neat and very easy to implement.

      Cheers,
      Darren :)

        I ran into a situation at work where I needed some kind of locking, and I too used adamk's solution, and it worked like a charm. It even worked over NFS! Thanks, adamk.

Log In?
Username:
Password:

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

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

    No recent polls found