Linux | bash | script | i am running self test

Linux:

# FLOCK STUFF
PIDFILE=/var/run/$(basename $0)
exec 100>$PIDFILE
flock -n 100 || exit 1
echo $ > $PIDFILE
# ... your code ...
test -r $PIDFILE && rm -f $PIDFILE

FreeBSD:

Install flock

Other BSD:

THATSME="$(ps wwaux | grep -w $ | grep -v grep | awk '{$1=$2=$3=$4=$5=$6=$7=$8=$9=$10="";print $0}' | sed 's|^\s*||')" && ps wwaux | grep -vw $ | grep -v grep | grep "$THATSME" > /dev/null && exit 2