I want to run something via cron which will take anywhere from less than a minute to a day or so (possibly). Most of the time it will be well under an hour, and I'd like it to run hourly, except if it's still running, in which case nothing more need be done.
What the best way to do that?
Kevin
#!/bin/bash TURD=/var/run/mycron.lock if [ -f $TURD ]; then exit fi touch $TURD # do stuff rm $TURD
(yes, there's a potential race condition. Probably doesn't matter though)
Sean
On Thu, Apr 1, 2010 at 1:06 PM, Kevin McGregor kevin.a.mcgregor@gmail.comwrote:
I want to run something via cron which will take anywhere from less than a minute to a day or so (possibly). Most of the time it will be well under an hour, and I'd like it to run hourly, except if it's still running, in which case nothing more need be done.
What the best way to do that?
Kevin
Roundtable mailing list Roundtable@muug.mb.ca http://www.muug.mb.ca/mailman/listinfo/roundtable
On 04/01/2010 01:18 PM, Sean Walberg wrote:
#!/bin/bash TURD=/var/run/mycron.lock if [ -f $TURD ]; then exit fi touch $TURD # do stuff rm $TURD
I find it helpful to have: ... TURD=/var/run/mycron.lock trap "rm -f $TURD" 0 ...
So that the file will be removed when the shell exits.
I have also used the procmail 'lockfile' command for situations where I wanted a shorter term lock.
Peter
On 2010-04-01 13:06, Kevin McGregor wrote:
I want to run something via cron which will take anywhere from less than a minute to a day or so (possibly). Most of the time it will be well under an hour, and I'd like it to run hourly, except if it's still running, in which case nothing more need be done.
What the best way to do that?
This came up on the CentOS-mirror list back in February. Proposed solutions included...
http://djlab.com/2009/10/lockfiles-in-bash/ [similar to Sean's solution] http://code.google.com/p/withlock/ [requires Python 2.4] flock(1) [may not be available other than on Linux?]
Hope this helps.
On 2010-04-01 Kevin McGregor wrote:
I want to run something via cron which will take anywhere from less than a minute to a day or so (possibly). Most of the time it will be well under an hour, and I'd like it to run hourly, except if it's still running, in which case nothing more need be done.
To not worry about races, and still keep things simple:
lock=~/lockfile-whatever lockfile -r 0 -l 86400 $lock >/dev/null 2>&1 || exit do_work rm -f $lock
see man lockfile
may not be terribly portable beyond linux(?)
Play with the -l option or omit it. For your case you'd want to set it to the absolute max secs it could possibly take your program to run.
Perl also has a ways to do locking.
Note: "Lockfile is NFS-resistant": I have no idea what they mean by that, will it work or not? Pretty nifty if they have it working on NFSvX where X<4.