[lug] Fwd: Simple counter?

Jeffrey S. Haemer jeffrey.haemer at gmail.com
Sat May 25 11:37:16 MDT 2013


>
> My solution would be to use a text file for persistence, and a simple
> program (without gotos ;)) that uses semaphores (you could think of
> semaphores as system wide mutexs) to coordinate access to the file. This
> solution assumes a local file mounted on a single computer. For multiple
> nodes in a network, the earlier suggestion of using a database may be the
> easiest solution.


Sure. We're using SQL now to do it.

And if I'm going to roll my own, my options are legion. For example, as an
SCM guy I know that SVN atomically numbers commits, so I could even use
Subversion version numbering.

"When all you have is a hammer ...."

Of course, I'd have to be deeply disturbed to implement something like that
(code follows), though it would work across users, reboots, and, with a
little tweaking, over Teh Internets.

But I'm in search of a single, simple, *existing* Linux command.

===

#!/bin/bash -eu
# makecounter: create a repo
#  whose revision numbers will be the counter

counter_db=/usr/share/counter.svn
[ -d $counter_db ] && rm -r $counter_db
svnadmin create $counter_db
chmod -R 777 $counter_db

# initialize it
counter_uri=file://$counter_db
tmp_d=/tmp/$$
svn co $counter_uri $tmp_d
trap 'rm -rf $tmp_d' EXIT

cd $tmp_d
echo $RANDOM > r
svn add r
svn ci -m"initial"


and


#!/bin/bash -u

# usecounter: use the counter created by makecounter


die() { echo "$@" >&2; exit 1; }

# could be on the network somewhere
counter_db=/usr/share/counter.svn
counter_uri=file://$counter_db

# scratch copy whereever suits your fancy
counter_d=/usr/tmp/counter_$LOGNAME

[ -f $counter_d ] ||
  svn co -q $counter_uri $counter_d
cd $counter_d

# give it a few tries, in case there's contention
for i in {0..10}; do
  svn -q update
  echo $RANDOM > r  # force a change
  svn ci -q -m'bump' && break
done
[ "$(svn -q status)" ] &&
  die "Can't commit change in '$counter_d'"

svn -q update
svnversion


-- 
Jeffrey Haemer <jeffrey.haemer at gmail.com>
720-837-8908 [cell], http://seejeffrun.blogspot.com [blog],
http://www.youtube.com/user/goyishekop [vlog]
*פרייהייט? דאס איז יאַנג דינען וואָרט.*
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lug.boulder.co.us/pipermail/lug/attachments/20130525/3a648b6e/attachment.html>


More information about the LUG mailing list