Hit count on a shell script

I have a unix shell script (ex.sh) written.
How to find out how many users (incl. myself) have run this .sh ?
I can insert code snipet at top of script if need be.

  • Ravi

What is your system? What is your shell?

You have to stop people from using your count file at the same time. Not knowing your system I use a fifo as a sem, you have to succeed in creating it before you can read or write the count file. Not ideal, and someone could DOS it(leave junk in /tmp/script-fifo so everyone waits forever). A database of some sort would be better but I have no idea what you have.

# Use a fifo as an ad-hoc semaphore.
# Only one script can create it at a time, the rest
# will wait.
while ! mkfifo /tmp/script-fifo 2> /dev/null
do
        sleep 1
done

COUNT=0
[ -f /path/to/countfile ] && read COUNT < /path/to/countfile
((COUNT++))
echo $COUNT > /path/to/countfile
rm -f /tmp/script-fifo

echo "script has been called $COUNT times"

...

/path/to/countfile must be a file which is writable by anyone running the script.

This one worked for me. No probs. Thx
I need to tweak to store the user-id's of all persons that ran this.
May be append the userid and COUNT++ into that file instead of overwriting
can we dothis

  • Ravi