We don't bite newbies here... much | |
PerlMonks |
Re: Preventing multiple instances (References on flock and running one copy of a script at a time)by eyepopslikeamosquito (Archbishop) |
on Dec 17, 2020 at 08:32 UTC ( [id://11125328]=note: print w/replies, xml ) | Need Help?? |
Here's some sample code, using Perl's flock function, that I've used for many years on both Unix and Windows, to ensure only one copy of a script is running at a time (update: run on local file systems only, not NFS). You can easily test its behaviour by running the little test program above in two different terminal sessions (and either waiting for the sleep to end or manually killing one of the processes). Apart from providing portable locking across Unix and Windows, flock has long been a favourite of Perl poets, as beautifully shown by pjf in this immortal line: from his classic poem my @chickens (by the way, pjf is one of the few monks I've met in real life; in addition to running chickens in his backyard, he has a keen interest in picking and eating unusual and delicious wild plants you won't find in any supermarket ... not for the faint-hearted, you need a keen eye and expert knowledge to avoid being poisoned). References Added Later
In Section
Seekers of Perl Wisdom
|
|