[nycbug-talk] two quick things

mikel king mikel.king at olivent.com
Thu Jan 5 21:34:45 EST 2012


On Jan 5, 2012, at 9:24 PM, Matthew Story wrote:

> 
>> 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


Nice update.

I use this method for more than rsyncs though.

I just like to avoid using grep to search for something and the piping it into awk where possible. Especially since awk can do all the work in the first place. 

Cheers,
m


More information about the talk mailing list