geektron has asked for the wisdom of the Perl Monks concerning the following question:

I have a job that runs on the half-hour via cron to update a local database from a remote (ugh) ODBC-based thing. Because I recently had to introduce some pauses ( via sleep(1) to try and compensate for the remote server's latency ( it's swapping out like mad ... and errors out too frequently; my update never gets completed ), the job can now take longer than the window. I could run the updater on the hour instead ( which I may do if I can't find another solution ), but for now, I have something like this:
if (-e "/tmp/.updLock") { open(FILE, "/tmp/.updLock"); my $line = <FILE>; close(FILE); chomp $line; + + warn "PROCESS $line already running \n\n"; + + my @ps = `ps -p $line`; my $count = @ps; if ($count == 1) { # IT's FAKING IT!! + + warn "PROCESS $line not actually running. Lockfile deleted. \n\ +n"; unlink("/tmp/.updLock"); } + + # we're still updating from last time + + die "$0 still running\n\n" if $count > 1; } else { open(FILE, "> /tmp/.upLock"); print FILE $$; close(FILE); }
Thing is, my cron output sends me the warn and die messages, but the script is *still* running.

I do have an END block, but all is does is: $dbh->disconnect() if defined( $dbh );

Replies are listed 'Best First'.
Re: die; not really dying
by UnderMine (Friar) on Mar 05, 2004 at 17:17 UTC
    I would recommend 'flock' the file so that that the second process can detect the difference between the first process still runnning (ie second process can see the file but not get a flock on in) and the first process dying (ie file still exists but flock can be obtained).
    places to looks for starters are :-
    • perldoc -f flock
    • perldoc perlopentut

    UnderMine
      i had forgotten about flock. thanks.
Re: die; not really dying
by bluto (Curate) on Mar 05, 2004 at 17:40 UTC
    By '*still* running' I'm assuming you mean the script that has executed the 'die'. If this is the case, I'd suspect your END block of hanging. One way to test this is to print/log the output at the beginning and end of the END block.

    As UnderMine suggests, I'd suggest you use a lock file instead of trying to 'ps' the process since a lock file is automatically unlocked when a process dies. It is always possible, and I even seen cases of this, that the process you are checking has actually died and that another arbitrary process has started with the same PID.

    bluto