Need to execute a script by one user at a time

Hi ,
I have a script which everyone have access but I need that the script should be ran by one user at a time. The second user who is trying to execute the script should get a message stating that the user is already executing the script. Is there any way to achieve this.?

Thanks in advance.

Use one file to maintain the script status.

It is just hint.
Cons - If you use ctrl c in middle of script then next time you can't run the script even if script is not running.
Solution - Get PID at the start of the script and then use that to check if the script is still running or not.( this i left for your exercise)

try something this..

if [[ ! -f test_script_check ]]
then
echo "test2.sh" > test_script_check
echo "script is running"
sleep 100
rm test_script_check
else
echo "script is already running"
fi

You can overcome the "con" above by setting a trap so the trap will remove the check file upon exit (no matter how you exit):

trap 'rm test_script_check' 0 2 3 9 15

More info on signals that can be caught:
Unix - Signals and Traps

Also you could write the current user into the check file, and if someone else tries to run it, the error message could print who is currently running it.