in reply to Re^2: Cron Jobs That Run For Too Long
in thread Cron Jobs That Run For Too Long
But wait, it gets better again!
You know how you can have that nifty __DATA__ block at the end of your script? It turns out you can lock that too :)
I've used this a number of times and it works just great.
#!/usr/bin/perl
use strict;
use Fcntl 'LOCK_EX', 'LOCK_NB';
unless ( flock DATA, LOCK_EX | LOCK_NB ) {
print STDERR "Found duplicate script run. Stopping\n";
exit(0);
}
...
1;
### DO NOT REMOVE THE FOLLOWING LINES ###
__DATA__
This exists to allow the locking code at the beginning of the file to work.
DO NOT REMOVE THESE LINES!
You know how you can have that nifty __DATA__ block at the end of your script? It turns out you can lock that too :)
I've used this a number of times and it works just great.
#!/usr/bin/perl
use strict;
use Fcntl 'LOCK_EX', 'LOCK_NB';
unless ( flock DATA, LOCK_EX | LOCK_NB ) {
print STDERR "Found duplicate script run. Stopping\n";
exit(0);
}
...
1;
### DO NOT REMOVE THE FOLLOWING LINES ###
__DATA__
This exists to allow the locking code at the beginning of the file to work.
DO NOT REMOVE THESE LINES!
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^4: Cron Jobs That Run For Too Long
by LanX (Saint) on Apr 23, 2021 at 16:02 UTC | |
by Bod (Parson) on Apr 23, 2021 at 17:52 UTC | |
by LanX (Saint) on Apr 23, 2021 at 19:21 UTC | |
by Bod (Parson) on Apr 24, 2021 at 11:56 UTC | |
by LanX (Saint) on Apr 24, 2021 at 15:34 UTC | |
Re^4: Cron Jobs That Run For Too Long
by astroboy (Chaplain) on Dec 23, 2005 at 18:40 UTC | |
Re^4: Cron Jobs That Run For Too Long
by Anonymous Monk on Jul 12, 2007 at 15:36 UTC | |
by marto (Cardinal) on Jul 12, 2007 at 15:46 UTC | |
Re^4: Cron Jobs That Run For Too Long
by Anonymous Monk on Oct 21, 2016 at 19:08 UTC |
In Section
Seekers of Perl Wisdom