Just putting this here because it helped me out today.  How do you ensure (on #linux ) that only one instance of a script is running, without race conditions or having to clean up lock files?  Simple:

#!/bin/sh                                                                       

(
    if ! flock -n -x 0
    then
        echo "$$ cannot get flock"
        exit 0
    fi
    echo "$$ start"
    sleep 10 # for testing.  put the real task here
    echo "$$ end"
) < $0
Shared publiclyView activity