[nycbug-talk] two quick things

Matthew Story matt at tablethotels.com
Thu Jan 5 21:24:44 EST 2012

> Or just not use grep at all. 

indeed ... for the uninitiated: http://partmaps.org/era/unix/award.html#grep

> I use this one to capture PIDs of interest. Like to ensure that my rsync job to a specific host has only one job at a time. Substitute what ever entity you are searching for between the $0's and let awk work it's magick.

If you really want to ensure that your rsync job to a specific host has only one job at a time ... use lockf (FreeBSD ... for linux I think it's flock, or you can use setlock if you're so inclined as to install daemontools).  It's the simplest and most reliable way to guarantee exclusivity.

lockf -t 0 -k /tmp/specific-hostname-rsync.lock rsync --stats -avzr ...

If you are cool with failure on any given try (e.g. if the job runs every minute and might fail normally 5 or 6 times a day ...) check out the -s option which will make it silently fail.  the awk one-liner has a race-condition if you're using it to detect exclusivity:

ps ax | awk '/rsync --stats -avzr/ && /[a]wk/ { count++ } count > 1 { exit 1 }' && rsync --stats -avzr ...

then you're exposing yourself to a (albeit unlikely) race condition

More information about the talk mailing list