Link to home
Start Free TrialLog in
Avatar of srikanthradix
srikanthradixFlag for United States of America

asked on

How to set Semaphore

We have a ksh shell script. Sometimes, this ksh script is executed multiple times due to some error in the calling program which we do not have control over. Until that is fixed, we are planning to use semaphore type control so that it won't be executed twice.
I got a semaphore example from "http://www.csm.astate.edu/~rossa/semab.html" semabinit.c program.
How to have semaphore type control over ksh shell script?
Avatar of gheist
gheist
Flag of Belgium image

kill -0 `cat /var/run/myscript.pid || /opt/sbin/script.sh &\
 echo $! > /var/run/myscript.pid
SOLUTION
Avatar of gheist
gheist
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Explanation:
Check if process with PID in file exists by sending it SIGNULL
|| - if return <> 0
Launch script in background
store new PID into file

ASKER CERTIFIED SOLUTION
Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of srikanthradix

ASKER

Thanks for solution. it is working perfectly fine.