Next | File Locking | 6 |
Suppose you have an hourly cron job
But you don't want two instances running at the same time
If the 8 AM job is still running at 9 AM, the 9 AM job should be cancelled
Easy solution:
open SELF, "< $0" or die ...; flock SELF, LOCK_EX | LOCK_NB or exit;
Try to get a lock on the program source code file
If it's already locked, another instance is still running, so exit
When the process that holds the lock exits, it releases the lock automatically
flock DATA, LOCK_EX | LOCK_NB or exit; ... __DATA__
Next | Copyright © 2003 M. J. Dominus |